views:

1906

answers:

6

Is there a way to plot a chart with google chart api so that the X-axis values are days in a month?

I have data points that are not provided with the same frecuency. For example: Date - Value 1/1/2009 - 100 1/5/2009 - 150 1/6/2009 - 165 1/13/2009 - 200 1/20/2009 - 350 1/30/2009 - 500

I want to make a chart that will separate each data point with the relative distance based on time during a month. This can be done with Excel, but any ideas of how to calculate it and display it with Google chart?

Other free solutions similar to Google chart or a free library that can be used with asp.net aslo welcomed.

A: 

This can be done with Google charts quite easily, but your application must calculate the labels

The chxl chart x label parameter is the one you need. The following example labels a y-axis with numbers in 50 steps, and the bottom with dates

chxl=0:|0|50|100|150|200|250|300|350|400|450|500|1:|16/01/2009|26/01/2009|6/02/2009
David Caunt
He isn't asking about labels, he's asking about getting the data to be at an x-axis position relative to the date's respective position in the overall time range.
Dustin Fineout
ok. But won't the data points 1 (16/1), 2(26/1) and 3(6/2) be separated in the output chart with the same distance even tough there are 10 days between the first 2 and 12 between the last 2?
jvanderh
Exactly Dustin, that's what I mean. Up to know what I'm doing is repeating the previous date value as many times as needed until a new date is available. This way I have a fixed amount of data points (30 for example) and many values repeated.
jvanderh
+7  A: 

I have done this on my ReHash Database Statistics chart (even though the dates turned out to be evenly spaced, so it doesn't exactly demonstrate that it's doing this).

First, you want to get your overall time period, which will be analogous to the overall width of your chart. To do this we subtract the earliest date from the latest. I prefer to use Unix-epoch timestamps as they are integers and easy to compare in this way, but you could easily calculate the number of seconds, etc.

Now, loop through your data. For each date we want the percentile in the overall period that the date is from the beginning (i.e. the earliest date is 0, the latest is 100). For each date, you first want to calculate the distance of the present date from the earliest date in the data set. Essentially, "how far are we from the start". So, subtract the earliest date from the present date. Then, to find the percentile, we divide the distance of the present date by the overall time period, and then multiply by 100 and truncate or round any decimal to give our integral x-coordinate.

And it is as simple as that! Your x-values will range from 0 (the left-side of the chart) to 100 (the right side) and each data point will lie at a distance from the start respective of it's true temporal distance.

If you have any questions, feel free to ask! I can post pesudocode or PHP if desired.

Dustin Fineout
makes sense...even better than what I was doing, and sounds simple to implement...thanks!!!
jvanderh
That works pretty good. Question is how to ensure the right x-labels. I have a left-skewed set of data point I want the scale to represent that. If I just the divide the whole range by lets say five the x-label will not correspond to the actual data point.
Christian Stade-Schuldt
+1  A: 

I'm not sure how to post this as a comment rather than an answer but I would love to see some PHP code on this. Thanks in advance.

Knix
You can't leave comments until you have 50 rep points. I recommend you ask it as a new question and reference this one. Dustin provided the concept of how to do it, but I wrote in vb.net and don't have any PHP knowledge to help you, but I'm sure others can.
jvanderh
Thank you for the suggestion and I apologize trying to post my comment as an answer.
Knix
If you posted as a question I will happily provide PHP code.
Dustin Fineout
A: 

Exactly what I was looking for! Thanks for asking the question, and thanks very much for the answer!

Darran
+1  A: 

It looks like you can now do this with advanced graphs:

http://code.google.com/apis/visualization/documentation/gallery/annotatedtimeline.html

BrianAdkins
A: 

Hai. I'm thinking along the same lines as you. I can foresee problems where labels overwrite each other, and can think of only two approaches.

1) figure out how many characters in each date,, depending on format (mon/tue, monday/tuesday, 1 jan/1 feb, 1st january, 29th february, 14/2/2010 ..etc) then calculate how many labels you can fit across your chart width (which might get gnarly, involving char width in pixels (easier for fixed fonts), or just a few trial & errors).

Of course, your labels might might align directly with any data.

2) use multiple X-axis labels and place your dates vertically.

LeonixSolutions