【问题标题】:Double Y axis ticks for Google Charts谷歌图表的双 Y 轴刻度
【发布时间】:2023-04-03 10:18:01
【问题描述】:

我正在尝试为双 y 轴线图设置刻度,但是该图不会加载,它加载的内容完全相同。任何帮助将不胜感激

目标是设置价格变动:[0.002, 0.004。 0.006。 0.008],音量增加 1000

价格也有问题,例如:0.00242、0.00521 都显示为 0.1

<?php
$sql = "SELECT Timestamp, LastPrice, Volume FROM vol";
$result = $dbconnect->query($sql);
?> 
 
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

<div id="chart_div"></div>
   
<script type="text/javascript">
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart);

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

    function drawChart() {

      var button = document.getElementById('change-chart');
      var chartDiv = document.getElementById('chart_div');
		  
		  
		  
        var data = google.visualization.arrayToDataTable([
                ['Timestamp','LastPrice','Volume'],
                <?php
                    while($row = mysqli_fetch_assoc($result)){
                        echo "[           '".$row["Timestamp"]."', ".$row["LastPrice"].", ".$row["Volume"].",    ],";
                    }
					echo $row["LastPrice"];
                ?>
        ]);

        var materialOptions = {
			
		chart: {
          
        },
        width: 600,
        height: 300,
		
        series: {
          // Gives each series an axis name that matches the Y-axis below.
          0: {axis: 'LastPrice' },
          1: {axis: 'BaseVolume'}
        },
		
		vAxis: {1: {ticks:[0, 0.002, 0.004, 0.006]} },
		
        axes: {
          // Adds labels to each axis; they don't have to match the axis names.
          y: {
            LastPrice: {label: 'Price'}, 
            BaseVolume: {label: 'Volume'}
			
          }
        }
		
      };

     

      function drawMaterialChart() {
        var materialChart = new google.charts.Line(chartDiv);
        materialChart.draw(data, materialOptions);
        button.innerText = 'Classic';
        button.onclick = drawClassicChart;
      }


      drawMaterialChart();

    }
	
</script>

【问题讨论】:

    标签: javascript charts google-visualization


    【解决方案1】:

    材质图表不支持几个配置选项,包括...

    {hAxis,vAxis,hAxes.*,vAxes.*}.ticks

    见 --> Tracking Issue for Material Chart Feature Parity

    相反,建议使用带有以下选项的 Classic 图表...

    theme: 'material'


    对于双 y 轴图表,使用 series 选项指定目标轴

      series: {
        1: {
          targetAxisIndex: 1,
        }
      },
    

    使用带有e的选项vAxes,为每个y轴指定ticks

      vAxes: {
        0: {
          ticks:[0, 1000, 2000, 3000],
          title: 'Last Price'
        },
        1: {
          ticks:[0, 0.002, 0.004, 0.006],
          title: 'Base Volume'
        }
      }
    

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

    google.charts.load('current', {
      callback: function () {
        var data = new google.visualization.DataTable({
          cols: [
            {label: 'x', type: 'string'},
            {label: 'y0', type: 'number'},
            {label: 'y1', type: 'number'}
          ],
          rows: [
            {c:[{v: 'row 0'}, {v: 1800}, {v: 0.00242}]},
            {c:[{v: 'row 1'}, {v: 2200}, {v: 0.00521}]},
            {c:[{v: 'row 2'}, {v: 2800}, {v: 0.00343}]},
            {c:[{v: 'row 3'}, {v: 2800}, {v: 0.00441}]},
            {c:[{v: 'row 4'}, {v: 2300}, {v: 0.00532}]}
          ]
        });
    
        var container = document.getElementById('chart');
        var chart = new google.visualization.LineChart(container);
        chart.draw(data, {
          width: 600,
          height: 300,
          series: {
            1: {
              targetAxisIndex: 1,
            }
          },
          theme: 'material',
          vAxes: {
            0: {
              ticks:[0, 1000, 2000, 3000],
              title: 'Last Price'
            },
            1: {
              ticks:[0, 0.002, 0.004, 0.006],
              title: 'Base Volume'
            }
          }
        });
      },
      packages: ['corechart']
    });
    <script src="https://www.gstatic.com/charts/loader.js"></script>
    <div id="chart"></div>

    【讨论】:

    • 你是一个传奇人物,工作就像一种享受,这很有意义
    • 如果可以的话,还有一个问题,我的数据在 Y 轴上被正确表示,但是当我将鼠标悬停在它上面时,它会将所有内容四舍五入而不是保留价格,所以不是0.00492123 它将是 0.005,对于 0.005213 它也只会显示 0.005。任何帮助将不胜感激,谢谢!
    • 找到了答案,为任何偶然发现这个 var formatter = new google.visualization.NumberFormat({ fractionDigits: 15 }); formatter.format(数据,1);再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-19
    相关资源
    最近更新 更多