简体   繁体   English

如何更改刻度值GOOGLE CHARTS

[英]How to change tick value GOOGLE CHARTS

This is my column chart 这是我的柱形图

在此处输入图片说明

What I want is to change tick 4th to be what ever value that is in the database, which I have set in a variable $dancerplace . 我想要将第4个刻度更改为数据库中的任何值,该值已在变量$dancerplace so for this question's sake, I want the place to look like it's in 4th place like in the image, but visually it should be 9th. 因此,出于这个问题,我希望该位置看起来像图像中的第4位,但在视觉上应为第9位。

And for 5th it should always be 60th. 对于第五名,它应该永远是第六十名。 so if anyone gets a place between 4 and 60 the gold column should adjust between those two ticks. 因此,如果有人获得4到60之间的位置,则金栏应在这两个刻度之间进行调整。 I want it to look as it does in the picture. 我希望它看起来像图片一样。

When I add 60 ticks then the green columns are all too close together without a noticeable gap between the ticks, see pic below 当我添加60个刻度时,绿色的列都太靠近了,刻度之间没有明显的间隙,请参见下图

在此处输入图片说明

Here is my code: 这是我的代码:

 // Load the Visualization API and the corechart package.
  google.charts.load('current', {'packages':['corechart']});

  // Set a callback to run when the Google Visualization API is loaded.
  google.charts.setOnLoadCallback(drawChart);

  // Callback that creates and populates a data table,
  // instantiates the pie chart, passes in the data and
  // draws it.
  function drawChart() {
    var maxPlace = 4;

  var data = google.visualization.arrayToDataTable([
    ['Competitors', 'Competitors', {type: 'string', role: 'style'}, 
{type: 'string', role: 'tooltip'}],
['$dancerName', 4, 'color: #D4AF37', "$dancerName Placement: 2nd Place Overall Placement: 3%"],
['1st Pl Winner', 1, 'color: #91b224', "1st Place Winner Overall Placement: 1.6%"],
['2nd Pl Winner', 2, 'color: #91b224', "2nd Place Winner Overall Placement: 3%"],
['3rd Pl Winner', 3, 'color: #91b224', "3rd Place Winner Overall Placement: 5%"]
  ]);

 var view = new google.visualization.DataView(data);
 view.setColumns([0, {
calc: function (dt, row) {
  var place = dt.getValue(row, 1);
  return (maxPlace - place + 1);
},
type: data.getColumnType(1),
label: data.getColumnLabel(1)
}, 2, 3]);

 var ticks = [];
for (var i = 0; i <= maxPlace; i = i + 1) {
addTick(i);
}

function addTick(i) {
var place = (maxPlace - i + 1).toFixed(0);
switch (place.substr(place.length - 1)) {
  case '1':
    place += 'st';
    break;

  case '2':
    place += 'nd';
    break;

  case '3':
    place += 'rd';
    break;


  default:
    place += 'th';
}
ticks.push({
  v: i,
  f: place
});
}



var options = {
title: 'Progress Report',
width: 600,
height: 550,
tooltip: {
  isHtml: true
},
colors: ['#91b224'],
legend: {
  position: 'bottom'
},
vAxis: {
//  baseline: 1,
  title: 'Competition Placement',
  ticks: ticks
}
};

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

Is this possible? 这可能吗?

After playing around with the code I finally figured out how to get it to work see my answer 在玩完代码之后,我终于想出了如何使它正常工作的方法,请参阅我的答案

PHP PHP

<?php
if(isset($_GET['id'])){
$childId=$_GET['id']; 
$chartsql = "SELECT `dancer_name`, `comp_entered`, `comp_lvl` `placement`, `dance_name` FROM `competition` WHERE id = '$childId'";
$chartres = mysqli_query($con,$chartsql);
$chartrow=mysqli_fetch_assoc($chartres);
    $dancerName = $chartrow["dancer_name"];
    $compName = $chartrow["comp_entered"];
    $compLvl = $chartrow["comp_lvl"];
    $placement = $chartrow["placement"];
    $danceName = $chartrow["dance_name"];
}
?>

$placement_num = intval($placement);

google charts 谷歌图表

<script type="text/javascript">

  // Load the Visualization API and the corechart package.
  google.charts.load('current', {'packages':['corechart']});

  // Set a callback to run when the Google Visualization API is loaded.
  google.charts.setOnLoadCallback(drawChart);

  // Callback that creates and populates a data table,
  // instantiates the pie chart, passes in the data and
  // draws it.
  function drawChart() {
     var maxPlace = 6;
     <?php
   if ($placement_num >=5 && $placement_num <=26) {
     $placement_num = 5;
       $newtick = intval($placement);
    } else if ($placement_num >=27 && $placement_num <=60) {
       $placement_num = 6;
   }
        ?> 

var data = google.visualization.arrayToDataTable([
['Competitors', 'Competitors', {type: 'string', role: 'style'}, {type: 'string', role: 'tooltip'}],

<?php

  echo "['$dancerName', $placement_num, 'color: #D4AF37', '$dancerName\'s ranking Placement: $placement Overall Placement: 3%'],";
  echo "['1st Pl Winner', 1, 'color: #91b224', '1st Place Winner Overall Placement: 1.6%'],";
  echo "['2nd Pl Winner', 2, 'color: #91b224', '2nd Place Winner Overall Placement: 3%'],";
  echo "['3rd Pl Winner', 3, 'color: #91b224', '3rd Place Winner Overall Placement: 5%']";

?> 
]);

var view = new google.visualization.DataView(data);
view.setColumns([0, {
calc: function (dt, row) {
  var place = dt.getValue(row, 1);
  return (maxPlace - place + 1);
},
type: data.getColumnType(1),
label: data.getColumnLabel(1)
}, 2, 3]);

var ticks = [];
  for (var i = 1; i <= maxPlace; i = i + 1) {
    addTick(i);
  }

function addTick(i) {
  var place = (maxPlace - i + 1).toFixed(0);
  switch (place.substr(place.length - 1)) {
    case '1':
      place += 'st';
      break;

    case '2':
      place += 'nd';
      break;

    case '3':
      place += 'rd';
      break;

<?php
        echo "case '5':
                place = '$newtick';
                place += 'th';
                break;
            ";

?>

    case '6':
      place += '0th';
      break;

    default:
      place += 'th';
  }
  ticks.push({
    v: i,
    f: place
  });
}

var options = {
  <?php echo "title: '$compName - $compLvl  - $danceName',";?>
  width: 720,
  height: 550,
  tooltip: {
    isHtml: true
  },
  colors: ['#91b224'],
  legend: {
  //position: 'bottom'
  },
  vAxis: {
    baseline: 1,
    title: 'Competition Placement',
    ticks: ticks
  }
};

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

Now the tick right above 60th will change depending on what is in the $newtick variable. 现在,第60号上方的刻度将根据$newtick变量中的内容而改变。

My chart looks like this now. 我的图表现在看起来像这样。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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