Google Charts轴标签

时间:2015-02-27 14:29:45

标签: javascript charts google-api

我试图在应用程序中绘制图表,我每个月都会显示数据,并希望在x轴上添加标签以表示年份,但是当我尝试自动添加标签时添加年份和月份使应用程序的外观变得混乱。

         var e=new google.visualization.arrayToDataTable([
            ["Year",county_1,county_2,"National"],
            ["Jan-10",med_jan_10_1,med_jan_10_2,med_jan_10_3],
            ["Feb-10",med_feb_10_1,med_feb_10_2,med_feb_10_3],
            ["Mar-10",med_mar_10_1,med_mar_10_2,med_mar_10_3],
            ["Apr-10",med_apr_10_1,med_apr_10_2,med_apr_10_3],
            ["May-10",med_may_10_1,med_may_10_2,med_may_10_3],
            ["Jun-10",med_jun_10_1,med_jun_10_2,med_jun_10_3],
            ["Jul-10",med_jul_10_1,med_jul_10_2,med_jul_10_3],
            ["Aug-10",med_aug_10_1,med_aug_10_2,med_aug_10_3],
            ["Sept-10",med_sep_10_1,med_sep_10_2,med_sep_10_3],
            ["Oct-10",med_oct_10_1,med_oct_10_2,med_oct_10_3],
            ["Nov-10",med_nov_10_1,med_nov_10_2,med_nov_10_3],
            ["Dec-10",med_dec_10_1,med_dec_10_2,med_dec_10_3],

            ["Jan-11",med_jan_11_1,med_jan_11_2,med_jan_11_3],
            ["Feb-11",med_feb_11_1,med_feb_11_2,med_feb_11_3],
            ["Mar-11",med_mar_11_1,med_mar_11_2,med_mar_11_3],
            ["Apr-11",med_apr_11_1,med_apr_11_2,med_apr_11_3],
            ["May-11",med_may_11_1,med_may_11_2,med_may_11_3],
            ["Jun-11",med_jun_11_1,med_jun_11_2,med_jun_11_3],
            ["Jul-11",med_jul_11_1,med_jul_11_2,med_jul_11_3],
            ["Aug-11",med_aug_11_1,med_aug_11_2,med_aug_11_3],
            ["Sept-11",med_sep_11_1,med_sep_11_2,med_sep_11_3],
            ["Oct-11",med_oct_11_1,med_oct_11_2,med_oct_11_3],
            ["Nov-11",med_nov_11_1,med_nov_11_2,med_nov_11_3],
            ["Dec-11",med_dec_11_1,med_dec_11_2,med_dec_11_3],

            ["Jan-12",med_jan_12_1,med_jan_12_2,med_jan_12_3],
            ["Feb-12",med_feb_12_1,med_feb_12_2,med_feb_12_3],
            ["Mar-12",med_mar_12_1,med_mar_12_2,med_mar_12_3],
            ["Apr-12",med_apr_12_1,med_apr_12_2,med_apr_12_3],
            ["May-12",med_may_12_1,med_may_12_2,med_may_12_3],
            ["Jun-12",med_jun_12_1,med_jun_12_2,med_jun_12_3],
            ["Jul-12",med_jul_12_1,med_jul_12_2,med_jul_12_3],
            ["Aug-12",med_aug_12_1,med_aug_12_2,med_aug_12_3],
            ["Sept-12",med_sep_12_1,med_sep_12_2,med_sep_12_3],
            ["Oct-12",med_oct_12_1,med_oct_12_2,med_oct_12_3],
            ["Nov-12",med_nov_12_1,med_nov_12_2,med_nov_12_3],
            ["Dec-12",med_dec_12_1,med_dec_12_2,med_dec_12_3],

            ["Jan-13",med_jan_13_1,med_jan_13_2,med_jan_13_3],
            ["Feb-13",med_feb_13_1,med_feb_13_2,med_feb_13_3],
            ["Mar-13",med_mar_13_1,med_mar_13_2,med_mar_13_3],
            ["Apr-13",med_apr_13_1,med_apr_13_2,med_apr_13_3],
            ["May-13",med_may_13_1,med_may_13_2,med_may_13_3],
            ["Jun-13",med_jun_13_1,med_jun_13_2,med_jun_13_3],
            ["Jul-13",med_jul_13_1,med_jul_13_2,med_jul_13_3],
            ["Aug-13",med_aug_13_1,med_aug_13_2,med_aug_13_3],
            ["Sept-13",med_sep_13_1,med_sep_13_2,med_sep_13_3],
            ["Oct-13",med_oct_13_1,med_oct_13_2,med_oct_13_3],
            ["Nov-13",med_nov_13_1,med_nov_13_2,med_nov_13_3],
            ["Dec-13",med_dec_13_1,med_dec_13_2,med_dec_13_3],

            ["Jan-14",med_jan_14_1,med_jan_14_2,med_jan_14_3],
            ["Feb-14",med_feb_14_1,med_feb_14_2,med_feb_14_3],
            ["Mar-14",med_mar_14_1,med_mar_14_2,med_mar_14_3],
            ["Apr-14",med_apr_14_1,med_apr_14_2,med_apr_14_3],
            ["May-14",med_may_14_1,med_may_14_2,med_may_14_3],
            ["Jun-14",med_jun_14_1,med_jun_14_2,med_jun_14_3],
            ["Jul-14",med_jul_14_1,med_jul_14_2,med_jul_14_3],
            ["Aug-14",med_aug_14_1,med_aug_14_2,med_aug_14_3],
            ["Sept-14",med_sep_14_1,med_sep_14_2,med_sep_14_3],
          ]);

            var b={
              title:"House Price Index by County:",
              curveType:"function",
              is3D:true,
              legend:"top",
              width:600,
              height:250,
              hAxis: {textPosition: 'none' }};
            var d=new google.visualization.LineChart(document.getElementById("chart_div"));
            d.draw(e,b)
        }

有人可以建议在图表上使用年份标签(2010:2014)吗?

1 个答案:

答案 0 :(得分:0)

您可能需要循环浏览数据以根据需要进行更改:

var exampleData = ["Jan-10",med_jan_10_1,med_jan_10_2,med_jan_10_3];
for (var i = 0; i < exampleData.length; i++) {
    var date = exampleData[i][0];
    var year = '20' + date.split('-')[1];
    exampleData[i][0] = year;
}