简体   繁体   中英

Google Charts LogScale not working

I'm using Google Charts to make a bar graph and I'm trying to get the graph to use a logscale on the vertical axis but it's not working for some reason. I've tried using logScale and scaleType but neither of them work.

Here's my code:

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

function drawChart() {
  var data = google.visualization.arrayToDataTable([
    ['Vendor', 'Nootropics Depot', 'Pure Nootropics', 'Peak Nootropics', 'Absorb Health'],
    ['ALCAR', 0.07, 0.20, 0.12, 0.24],
    ['Agmatine', 0.13, null, 0.23, null],
    ['Coluracetam', 13.80, 27.49, null, null],
    ['Oxiracetam', 0.47, 0.37, 0.58, 0.63],
    ['Phenibut', 0.22, null, 0.59, 0.60],
    ['Phenylpiracetam', 2.83, 3.33, 5.30, null],
    ['Polygala', 0.42, null, null, null],
    ['Pramiracetam', 1.67, 2.22, 1.60, 2.60],
    ['PRL-8-53', 23.99, null, null, null],
    ['Rhodiola Rosea 3% Rosavins', 0.23, 0.60, 0.32, 0.26],
  ]);

  var formatter = new google.visualization.NumberFormat({
    suffix: 'tablets',
    negativeColor: 'red',
    negativeParens: true
  });
  formatter.format(data, 0);
  var formatter = new google.visualization.NumberFormat({
    prefix: '$',
    negativeColor: 'red',
    negativeParens: true
  });
  formatter.format(data, 1);
  formatter.format(data, 2);
  formatter.format(data, 3);
  formatter.format(data, 4);

  var options = {
    chart: {
      title: 'Cheapest price per gram',
    },
    bars: 'vertical', // Required for Material Bar Charts.
    hAxis: {
      format: 'decimal',
    },
    vAxis: {
      title: 'Price',
      format: '$#',
      minValue: 0.1,
      logScale: true,
    },
    height: 400,
    colors: ['#1b9e77', '#d95f02', '#7570b3']
  };

  var chart = new google.charts.Bar(document.getElementById('chart_div'));

  chart.draw(data, google.charts.Bar.convertOptions(options));

}

JSFiddle page: https://jsfiddle.net/rm8vr1p8/

Not really sure what I'm doing wrong. I've even tried changing the values of the vertical axis but it just isn't working. What am I missing?

there are several options that are not supported by Material charts, including...
{hAxis,vAxis,hAxes.*,vAxes.*}.logScale

see --> Tracking Issue for Material Chart Feature Parity


Material = google.charts.Bar -- packages: ['bar']

Classic = google.visualization.ColumnChart -- packages: ['corechart']


there is an option for Classic charts to look similar to Material ...
theme: 'material

see following working snippet...

 google.charts.load('current', { packages: ['corechart'] }).then(function () { var data = google.visualization.arrayToDataTable([ ['Vendor', 'Nootropics Depot', 'Pure Nootropics', 'Peak Nootropics', 'Absorb Health'], ['ALCAR', 0.07, 0.20, 0.12, 0.24], ['Agmatine', 0.13, null, 0.23, null], ['Coluracetam', 13.80, 27.49, null, null], ['Oxiracetam', 0.47, 0.37, 0.58, 0.63], ['Phenibut', 0.22, null, 0.59, 0.60], ['Phenylpiracetam', 2.83, 3.33, 5.30, null], ['Polygala', 0.42, null, null, null], ['Pramiracetam', 1.67, 2.22, 1.60, 2.60], ['PRL-8-53', 23.99, null, null, null], ['Rhodiola Rosea 3% Rosavins', 0.23, 0.60, 0.32, 0.26], ]); var formatter = new google.visualization.NumberFormat({ suffix: 'tablets', negativeColor: 'red', negativeParens: true }); formatter.format(data, 0); var formatter = new google.visualization.NumberFormat({ prefix: '$', negativeColor: 'red', negativeParens: true }); formatter.format(data, 1); formatter.format(data, 2); formatter.format(data, 3); formatter.format(data, 4); var options = { theme: 'material', title: 'Cheapest price per gram', hAxis: { format: 'decimal' }, vAxis: { title: 'Price', format: '$#', minValue: 0.1, logScale: true }, height: 400, colors: ['#1b9e77', '#d95f02', '#7570b3'] }; var chart = new google.visualization.ColumnChart(document.getElementById('chart_div')); chart.draw(data, options); }); 
 <script src="https://www.gstatic.com/charts/loader.js"></script> <div id="chart_div"></div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM