简体   繁体   中英

How can i modify googlechart DataView annotation from arrayToDataTable that get data from json encode?

my PHP :

$chart = array(
array('axisX'=>'0','value'=>$temp_value[0]),
array('axisX'=>'1','value'=>$temp_value[1]),
array('axisX'=>'2','value'=>$temp_value[2]),
array('axisX'=>'3','value'=>$temp_value[3]),
array('axisX'=>'4','value'=>$temp_value[4]),
array('axisX'=>'5','value'=>$temp_value[5]),
array('axisX'=>'6','value'=>$temp_value[6]),
array('axisX'=>'7','value'=>$temp_value[7]),
array('axisX'=>'8','value'=>$temp_value[8]),
array('axisX'=>'9','value'=>$temp_value[9]));

function convertDataToChartForm($chart)
{
    $newData = array();
    $firstLine = true;

    foreach ($chart as $dataRow)
    {
        if ($firstLine)
        {
            $newData[] = array_keys($dataRow);
            $firstLine = false;
        }

        $newData[] = array_values($dataRow);
    }

    return $newData;
}

Thisi is my script :

var data = google.visualization.arrayToDataTable(<?php echo json_encode(convertDataToChartForm($chart));?>);

var view = new google.visualization.DataView(data);
      view.setColumns([0, 1,
                       { calc: "stringify",
                         sourceColumn: 1,
                         type: "string",
                         role: "annotation" }]);

what i want to try to achieve is i want to add "x" after the annotation? For example the annotation output is 9 and i want to it become "9x".

rather than using the built in "stringify" function,
you can use your own custom function.

the calc function will receive two arguments,
the data table, and the row index

var view = new google.visualization.DataView(data);
view.setColumns([0, 1, {
  calc: function (dt, row) {
    return dt.getFormattedValue(row, 1) + 'x';
  },
  type: "string",
  role: "annotation"
}]);

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