Google图表-酒吧系列标题不同的颜色 [英] Google Chart - Bar Series Title Different Color

查看:54
本文介绍了Google图表-酒吧系列标题不同的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含4个系列的图表(下面的Fiddle链接).当前,它具有Title1,Title2,Title3和Title4.我想为每个标题文本名称更改不同的颜色(不是栏);例如,Title1为红色,Title2为蓝色,Title3为绿色,Title4为黑色.有没有办法用不同的颜色更改所有这些标题?

I have a chart which has 4 series (Fiddle link below). Currently, it has Title1, Title2, Title3, and Title4. I want to change the different color for each title text name (Not the bar); for example, Title1 is red, Title2 is blue, Title3 is green, and Title4 is black. Is there a way to change all these titles with different colors?

https://jsfiddle.net/milacay/e4fe4hsz/21/

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test Google Chart</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {callback: drawChart, packages: ['corechart']});

function drawChart() {
    var array = [
      ["", "Today", "Goal"],
      ["Title1", 4553, 4151],
      ["Title2", 5560, 6150],
      ["Title3", 850, 920],
      ["Title4", 10505, 12320]
    ];

    var data = new google.visualization.arrayToDataTable(array);
    var formatTooltip = new google.visualization.NumberFormat({
        pattern : '#,##0'
    });
    formatTooltip.format(data, 1);
    formatTooltip.format(data, 2);

    var formatShort = new google.visualization.NumberFormat({
        pattern : 'short'
    });

    var view = new google.visualization.DataView(data);
    view.setColumns([0, 1, {
            calc : function (dt, row) {
              return formatShort.formatValue(dt.getValue(row, 1));
            },
            type : "string",
            role : "annotation"
        },
        2, {
            calc : function (dt, row) {
              return formatShort.formatValue(dt.getValue(row, 2));
            },
            type : "string",
            role : "annotation"
        },
    ]);

    var options = {

        chart: {
          title: ' ',
          animation: {
            duration: 2000,
            easing: "out",
            startup: true,
          }
        },
        chartArea: {right:0
                , width: "80%"
                , height: "80%" 
            },
        bar: { 
            groupWidth: 55  // Set the width for each bar
        },
        legend: {position:'top'},
        hAxis: {
                format: 'short',
                //title: 'Month',
                textStyle : { 
                    bold: true,
                    fontSize: 10 // fontsize for the vAxis label.
                    //color: 'darkblue',
                },
            },
        vAxis: {
                format: 'short',
                title: 'Progress To-Date',
                gridlines: { count: 8 } 
            },
        width:320, 
        height:300,
        bars: 'vertical',
        colors: ["lightblue", "lightgray"]

      };
    var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
    chart.draw(view, options);
}

</script>
</head>
<body>
<div id="chart_div"></div>
</body>
</html>

推荐答案

如果要更改轴标签的颜色,
当图表的'ready'事件触发时,手动更改这些值,
参见以下工作片段...

if you want to change the color of the axis labels,
change those manually when the chart's 'ready' event fires,
see following working snippet...

google.charts.load('current', {
    callback: drawChart,
    packages: ['corechart']
});

function drawChart() {
    var array = [
      ["", "Today", "Goal"],
      ["Title1", 4553, 4151],
      ["Title2", 5560, 6150],
      ["Title3", 850, 920],
      ["Title4", 10505, 12320]
    ];

    var data = new google.visualization.arrayToDataTable(array);
    var formatTooltip = new google.visualization.NumberFormat({
        pattern : '#,##0'
    });
    formatTooltip.format(data, 1);
    formatTooltip.format(data, 2);

    var formatShort = new google.visualization.NumberFormat({
        pattern : 'short'
    });

    var view = new google.visualization.DataView(data);
    view.setColumns([0, 1, {
            calc : function (dt, row) {
              return formatShort.formatValue(dt.getValue(row, 1));
            },
            type : "string",
            role : "annotation"
        },
        2, {
            calc : function (dt, row) {
              return formatShort.formatValue(dt.getValue(row, 2));
            },
            type : "string",
            role : "annotation"
        },
    ]);

    var options = {
        chart: {
          title: ' ',
          animation: {
            duration: 2000,
            easing: "out",
            startup: true,
          }
        },
        chartArea: {right:0
                , width: "80%"
                , height: "80%"
            },
        bar: {
            groupWidth: 55  // Set the width for each bar
        },
        legend: {position:'top'},
        hAxis: {
                format: 'short',
                //title: 'Month',
                textStyle : {
                    bold: true,
                    fontSize: 10 // fontsize for the vAxis label.
                    //color: 'darkblue',
                },
            },
        vAxis: {
                format: 'short',
                title: 'Progress To-Date',
                gridlines: { count: 8 }
            },
        width:320,
        height:300,
        bars: 'vertical',
        colors: ["lightblue", "lightgray"]

      };

    var container = document.getElementById('chart_div');
    var chart = new google.visualization.ColumnChart(container);

    google.visualization.events.addListener(chart, 'ready', function () {
      var colorIndex = 0;
      var colorPallette = ['red', 'blue', 'green', 'black'];
      Array.prototype.forEach.call(container.getElementsByTagName('text'), function (label) {
        if ((label.getAttribute('text-anchor') === 'middle') && (label.getAttribute('fill') !== '#404040')) {
          label.setAttribute('fill', colorPallette[colorIndex]);
          colorIndex++;
        }
      });
    });

    chart.draw(view, options);
}

<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

注意:标签颜色(#404040)用于区分x轴标签和注释标签...

note: the label color (#404040) is used to differentiate the x-axis labels from the annotation labels...

这篇关于Google图表-酒吧系列标题不同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆