【问题标题】:How to get rid of comma in tooltip data on Google chart?如何摆脱谷歌图表工具提示数据中的逗号?
【发布时间】:2018-09-21 10:24:30
【问题描述】:

我想去掉工具提示中分隔数字/日期的逗号,并希望将数据格式化为“2010”而不是“2,010”

    google.charts.load('current', {
callback: function () {
var rawData = [
    [2010, 100, 100],
    [2011, 105, 120],
    [2012, 111, 122],
    [2013, 122, 132],
    [2014, 131, 146],
    [2015, 139, 150],
    [2016, 143, 156],
];


var data = new google.visualization.DataTable({
  "cols": [
    {"id":"","label":"Date","type":'number'},
    {"id":"","label":"Black","type":'number'},
    {"id":"","label":"White","type":"number"}
  ]
});   

var options = {
    backgroundColor: 'transparent',
    focusTarget: 'category',
    fontName: 'Union',
    lineWidth: 3,
    colors: ['#000'],
    crosshair: { orientation: 'vertical', trigger: 'both', color: 'black' },
    tooltip: { isHtml: true},
    pointSize: 0,
    animation:{
    startup: true,
    duration: 300,
    easing: 'out'
  },
    legend: 'none',
    series: {
        0: { lineDashStyle: [4, 4],tooltip : false, color:'rgb(223, 119, 106)', enableInteractivity: false, format: '0000'},
        1: {color:'black', zIndex:5, format: '0000'},
    },
    hAxis: {
      format: '0000',
      gridlines: { color: 'transparent', count: 6 },
      textStyle: { fontSize: 14, color: 'black' },
      viewWindow: { min: 2010, max: 2016 }
  },
    vAxis:{ 
      gridlines: { count: 7 },
        textPosition: 'none',
      textStyle: { color: 'transparent' },
      viewWindow: { min: 100, max: 160 }
  },
    chartArea: { top: 110, left: 20, right: 200 },
};

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


drawChart();
setInterval(drawChart, 500);


var rowIndex = 0;
function drawChart() {
  if (rowIndex < rawData.length) {
    data.addRow(rawData[rowIndex++]);
    chart.draw(data, options);
  }
}
},
packages:['corechart']
});

<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div" style="width: 900px; height: 500px"></div>

我遇到的问题是在不影响数据的情况下维护动画。我想知道这是否可能? 我遇到的问题是在不影响数据的情况下维护动画。我想知道这是否可能?

【问题讨论】:

    标签: charts google-visualization tooltip linegraph


    【解决方案1】:

    您可以使用数字格式化程序...

    var formatYear = new google.visualization.NumberFormat({
      pattern: '0'
    });
    

    您只需要在添加每一行后格式化表格...

    function drawChart() {
      if (rowIndex < rawData.length) {
        data.addRow(rawData[rowIndex++]);
        formatYear.format(data, 0);
        chart.draw(data, options);
      }
    }
    

    请参阅以下工作 sn-p...

    google.charts.load('current', {
      callback: function () {
        var rawData = [
            [2010, 100, 100],
            [2011, 105, 120],
            [2012, 111, 122],
            [2013, 122, 132],
            [2014, 131, 146],
            [2015, 139, 150],
            [2016, 143, 156],
        ];
    
        var data = new google.visualization.DataTable({
          "cols": [
            {"id":"","label":"Date","type":'number'},
            {"id":"","label":"Black","type":'number'},
            {"id":"","label":"White","type":"number"}
          ]
        });
    
        var options = {
            backgroundColor: 'transparent',
            focusTarget: 'category',
            fontName: 'Union',
            lineWidth: 3,
            colors: ['#000'],
            crosshair: { orientation: 'vertical', trigger: 'both', color: 'black' },
            tooltip: { isHtml: true},
            pointSize: 0,
            animation:{
            startup: true,
            duration: 300,
            easing: 'out'
          },
            legend: 'none',
            series: {
                0: { lineDashStyle: [4, 4],tooltip : false, color:'rgb(223, 119, 106)', enableInteractivity: false, format: '0000'},
                1: {color:'black', zIndex:5, format: '0000'},
            },
            hAxis: {
              format: '0000',
              gridlines: { color: 'transparent', count: 6 },
              textStyle: { fontSize: 14, color: 'black' },
              viewWindow: { min: 2010, max: 2016 }
          },
            vAxis:{
              gridlines: { count: 7 },
                textPosition: 'none',
              textStyle: { color: 'transparent' },
              viewWindow: { min: 100, max: 160 }
          },
            chartArea: { top: 110, left: 20, right: 200 },
        };
    
        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
    
        drawChart();
        setInterval(drawChart, 500);
    
        var formatYear = new google.visualization.NumberFormat({
          pattern: '0'
        });
    
        var rowIndex = 0;
        function drawChart() {
          if (rowIndex < rawData.length) {
            data.addRow(rawData[rowIndex++]);
            formatYear.format(data, 0);
            chart.draw(data, options);
          }
        }
      },
      packages:['corechart']
    });
    <script src="https://www.gstatic.com/charts/loader.js"></script>
    <div id="chart_div"></div>

    【讨论】:

    • 非常感谢,我一直在为格式化程序选项苦苦挣扎。
    猜你喜欢
    • 2013-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多