Charts双Y轴线上的Y轴格式

Charts双Y轴线上的Y轴格式

本文介绍了Google Charts双Y轴线上的Y轴格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想格式化我的Google双Y轴折线图的Y轴。
这里我使用的代码:

I would like to format the Y Axis of my google Dual Y Axis Line Chart.Here the code I'm using:

 / code> 

use NumberFormat to format the data

这将设置工具提示的格式...

this will set the format of the tooltip...

// create formatter
var formatNumber = new google.visualization.NumberFormat({pattern: '#,##0.0'});

// format column 1 - Pressure
formatNumber.format(data, 1);

// format column 2 - Temperature
formatNumber.format(data, 2);

格式化y轴',将其添加到 materialOptions ...

to format both y-axis', add this to materialOptions...

vAxis: {
  format: '#,##0.0'
}

也建议使用 google.charts.Line.convertOptions

also recommend using google.charts.Line.convertOptions with Material charts

查看以下工作片段...

see following working snippet...

>

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

function drawChart() {
  var chartDiv = document.getElementById('chart_div');

  var data = new google.visualization.DataTable();
  data.addColumn('date', 'Date');
  data.addColumn('number', "Average Pressure");
  data.addColumn('number', "Average Temperature");

  data.addRows([
    [new Date(2016, 08, 29, 00, 03, 00), 1019.2, 23.7],
    [new Date(2016, 08, 29, 00, 06, 00), 1019.27, 23.6],
    [new Date(2016, 08, 29, 00, 09, 00), 1019.37, 23.6],
    [new Date(2016, 08, 29, 00, 12, 00), 1019.34, 23.6],
    [new Date(2016, 08, 29, 14, 33, 00), 1014.89, 30.8],
    [new Date(2016, 08, 29, 14, 36, 00), 1014.81, 30.6],
    [new Date(2016, 08, 29, 14, 39, 00), 1014.82, 30.8],
    [new Date(2016, 08, 29, 14, 42, 00), 1014.76, 31.1],
    [new Date(2016, 08, 29, 14, 45, 00), 1014.7, 31],
    [new Date(2016, 08, 29, 14, 48, 00), 1014.67, 30.6],
    [new Date(2016, 08, 29, 14, 51, 00), 1014.73, 31],
    [new Date(2016, 08, 29, 14, 54, 00), 1014.74, 30.7],
    [new Date(2016, 08, 29, 14, 57, 00), 1014.77, 30.5],
    [new Date(2016, 08, 29, 15, 00, 00), 1014.75, 30.1],
  ]);

  var formatPattern = '#,##0.0';
  var formatNumber = new google.visualization.NumberFormat({pattern: formatPattern});
  formatNumber.format(data, 1);
  formatNumber.format(data, 2);

  var materialOptions = {
    chart: {
      title: 'Average Pressure and Temperatures'
    },
    width: 1200,
    height: 600,
    series: {
      0: {axis: 'Pressure'},
      1: {axis: 'Temperature'}
    },
    axes: {
      y: {
        Temps: {
          label: 'Pressure'
        },
        Daylight: {
          label: 'Temps (Celsius)'
        }
      }
    },
    vAxis: {
      format: formatPattern
    }
  };

  function drawMaterialChart() {
    var materialChart = new google.charts.Line(chartDiv);
    materialChart.draw(data, google.charts.Line.convertOptions(materialOptions));
  }
  drawMaterialChart();
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

这篇关于Google Charts双Y轴线上的Y轴格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:04