Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Column Chart with date axis not working

I have a Column Chart with an x-axis value which is a date. This chart worked this morning but is suddenly broken and displaying "Bars series with value domain axis is not supported." as an error message. The website in question hasn't been updated in weeks.

My DataTable construction code looks like:

var data= new google.visualization.DataTable({
        "cols":[{"label":"Date","type":"date"},{"label":"New Users","type":"number"}],
        "rows":[{"c":[{"v":new Date(1325656800000),"f":null},{"v":1355,"f":null}]}]
    });

What can I do to my code to fix this?

like image 222
JeffreyABecker Avatar asked Jan 18 '12 19:01

JeffreyABecker


People also ask

Why is date axis not showing in Excel?

In the chart, right-click the category axis, and then click Format Axis. In the Format Axis pane, select the Axis Options tab. Expand Axis Options, and then under Axis Type, make sure Date axis is selected.

Why is my Excel chart not showing all dates?

Excel provides options to display dates on the axis that can fix this blank space. Do right-click on the Y-axis and select Format Axis. In the Format Axis menu find Axis Type and switch it to the Text axis value. Dates in the chart have no blank space, now.

Why is Excel not plotting data correctly?

If there's a value in the upper-left corner of the data set (A1 in this case), Excel fails to chart the data correctly. The fix is simple: remove the string in cell A1 and generate the chart again.

Why won't Excel let me change axis bounds?

If Excel detects that the data contains something different (such as text), then it doesn't present the Bounds option. So, check the data and make sure it actually contains dates, refresh (or regenerate) the chart, and the problem should disappear.


2 Answers

It's not a bug. Google Visualisation API has changed.

At http://code.google.com/apis/chart/interactive/docs/customizing_axes.html#Help they post some solutions to this problem. Using option:

strictFirstColumnType: false

can only be used as a temporary solution. Google says:

However, please bare in mind that this option is only available for limited time and will be removed in the near future.

The recommended solution is that you change your Date fields on x axis to String. I've achieved this by using formatter before adding value to the DateTable object.

var formatterMoney = new google.visualization.NumberFormat({suffix: ' zł', decimalSymbol: ',', groupingSymbol: ' '});  
var formatterDate = new google.visualization.DateFormat({pattern: 'dd.MM.yyyy'});

var data = new google.visualization.DataTable();

data.addColumn('string', 'order date'); //used to be date field here
data.addColumn('number', 'total amount');
data.addRow([formatterDate.formatValue(new Date('2011-12-20')),971793.93]); //used to be Date object, now is Date formated as String
data.addRow([formatterDate.formatValue(new Date('2011-11-30')),1.0]);
data.addRow([formatterDate.formatValue(new Date('2011-11-17')),1.0]);
data.addRow([formatterDate.formatValue(new Date('2011-10-27')),1.72]);
data.addRow([formatterDate.formatValue(new Date('2011-10-26')),10.27]);

var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
formatterMoney.format(data, 1);
chart.draw(data, {width: window.width, height: 400, hAxis: {direction: -1}});
like image 77
Michał Maciej Gałuszka Avatar answered Oct 19 '22 08:10

Michał Maciej Gałuszka


The problem is with the Date fields. I've converted the date field to a String and I'm using a String now. In case you are using formatters, you can format the value before supplying it to the DataTable:

formatter.formatValue(date)

I'm guessing this is a bug; I'll try to file a bug report.

like image 26
Miguel Ping Avatar answered Oct 19 '22 06:10

Miguel Ping