javascript - 谷歌条形图中的单个条形颜色

标签 javascript php json google-api bar-chart

我有一个从 json 获取数据的谷歌条形图。这是每月的客户流程图,它显示了该月的所有天数以及各自的客户流量。现在我希望显示最大客户流量的条形图为蓝色,其余部分为灰色。 例如:

^
|      ...
|-----Grey--------
|-----Blue------------
|-----Grey----------
|-----Grey--------
|-----Grey----------
|      ...
|__________________________________>

这是 json 的一部分:

$table = array();
$table['cols'] = array(
    /* define your DataTable columns here
     * each column gets its own array
     * syntax of the arrays is:
     * label => column label
     * type => data type of column (string, number, date, datetime, boolean)
     */
    // I assumed your first column is a "string" type
    // and your second column is a "number" type
    // but you can change them if they are not
    array('label' => 'Dates as in '.$mon, 'type' => 'string'),
    array('label' => 'Revenue', 'type' => 'number')
);

$rows = array();
while($r = mysql_fetch_assoc($query)) {
    $temp = array();
    // each column needs to have data inserted via the $temp array
    $temp[] = array('v' => $r['date']);
    $temp[] = array('v' => (int) $r['amount']); // typecast all numbers to the appropriate type (int or float) as needed - otherwise they are input as strings

    // insert the temp array into $rows
    $rows[] = array('c' => $temp);
}

// populate the table with rows of data
$table['rows'] = $rows;

// encode the table as JSON
$jsonTable = json_encode($table);

最佳答案

1)执行此操作的一种方法。

 var options  = {
    legend: { position: 'bottom', maxLines: 3 },
    bar: { groupWidth: '50%' },
    colors:[{color:'#FF9900', darker:'#FF9900'},{color:'#3366CC', darker:'#3366CC'},{color:'#DC3912', darker:'#DC3912'}]
  };

Google api 允许在编写脚本以启用和绘制图表时自定义条形图颜色。选项有不同的属性可供自定义。绘制图表时需要将上述代码添加到代码的脚本 block 中。我建议您访问以下链接。

2) 其他并由 google 推荐。

有三种不同的方式来选择颜色,数据表展示了所有这些:RGB值、英文颜色名称和类似CSS的声明:

var data = google.visualization.arrayToDataTable([
    ['Element', 'Density', { role: 'style' }],
        ['Copper', 8.94, '#b87333'],            // RGB value
    ['Silver', 10.49, 'silver'],            // English color name
    ['Gold', 19.30, 'gold'],
    ['Platinum', 21.45, 'color: #e5e4e2' ], // CSS-style declaration
      ]);

更多样式

  function drawChart() {
  var data = google.visualization.arrayToDataTable([
    ['Year', 'Visitations', { role: 'style' } ],
    ['2010', 10, 'color: gray'],
    ['2010', 14, 'color: #76A7FA'],
    ['2020', 16, 'opacity: 0.2'],
    ['2040', 22, 'stroke-color: #703593; stroke-width: 4; fill-color: #C5A5CF'],
    ['2040', 28, 'stroke-color: #871B47; stroke-opacity: 0.6; stroke-width: 8; fill-color: #BC5679; fill-opacity: 0.2'],
  ]);

Please refer here获取包含一些优秀示例的文档。

关于javascript - 谷歌条形图中的单个条形颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22642478/

相关文章:

javascript - 在 contenteditable div 中使用焦点元素?

php - oscommerce了解子类目产品布局设计的div结构

php - 使用 PHP 在文本正文中查找 3-8 个单词的常用短语

android - 获取错误 JSONException : No Value For

javascript - 如何在React js api中以小数形式传递对象值

Javascript:从 UL 选择列表

javascript - 如何在angular js中设置最大值和最小值?

php - 如何设置 Apache 以区分两个域?

jquery parseHTML 没有按预期工作

javascript - 错误错误 : Uncaught (in promise): Error: BrowserModule has already been loaded : angular 6