【问题标题】:Trying to figure out how to assign specific colors to specific data points in Highchart Pie chart试图弄清楚如何将特定颜色分配给 Highchart 饼图中的特定数据点
【发布时间】:2019-02-05 20:14:01
【问题描述】:

我有一种情况,如果数据点为空,我不希望包含该数据点,但我希望每个显示点的颜色无论如何都相同。因此,在我的种族示例中,一个特定的实例可能会产生亚裔、西班牙裔和其他人,而另一个可能会产生非裔美国人、亚裔和太平洋岛民。我希望这两种情况下的亚洲人都用相同的颜色表示,而不仅仅是前者颜色列表中的第一种颜色和后者颜色列表中的第二种颜色。

我尝试包含颜色内联,但无法使其正常工作。我包含了 PHP 代码,因此不会显示空选项。以下是该系列的部分代码。

    var series = [{
    type: 'pie',
    name: 'Ethnicity',
    colorByPoint: true,
    data: [
    <?php if($afam <> 0){echo"['African American', "  . $afam . "],";} ?>
    <?php if($asia <> 0){echo"['Asian', "  . $asia . "],";} ?>
    <?php if($hisp <> 0){echo"['Hispanic/Latino', "  . $hisp . "],";} ?>
    <?php if($naam <> 0){echo"['Native American', "  . $naam . "],";} ?>
    <?php if($other <> 0){echo"['Other', "  . $other . "],";} ?>
    <?php if($pais <> 0){echo"['Pacific Islander', "  . $pais . "],";} ?>
    <?php if($twop <> 0){echo"['Two or More', "  . $twop . "],";} ?>
    <?php if($whit <> 0){echo"['White', "  . $whit . "],";} ?>

    ]
    }];

【问题讨论】:

    标签: highcharts colors series


    【解决方案1】:

    您可以在数据中使用颜色内联,但您应该将数据定义为对象数组而不是数组数组,并使用 namey color 属性。在您的情况下,它将类似于以下内容(将 YOURCOLOR 替换为您独特的颜色名称或#hex):

    var series = [{
    type: 'pie',
    name: 'Ethnicity',
    colorByPoint: true,
    data: [
        <?php if($afam <> 0){echo"{'name': 'African American', 'y': "  . $afam . "},";} ?>
        <?php if($asia <> 0){echo"{'name': 'Asian', 'y': "  . $asia . ", 'color': 'YOURCOLOR'},";} ?>
        <?php if($hisp <> 0){echo"{'name': 'Hispanic/Latino', 'y': "  . $hisp . ", 'color': 'YOURCOLOR'},";} ?>
        <?php if($naam <> 0){echo"{'name': 'Native American', 'y': "  . $naam . ", 'color': 'YOURCOLOR'},";} ?>
        <?php if($other <> 0){echo"{'name': 'Other', 'y': "  . $other . ", 'color': 'YOURCOLOR'},";} ?>
        <?php if($pais <> 0){echo"{'name': 'Pacific Islander', 'y': "  . $pais . ", 'color': 'YOURCOLOR'},";} ?>
        <?php if($twop <> 0){echo"{'name': 'Two or More', 'y': "  . $twop . ", 'color': 'YOURCOLOR'},";} ?>
        <?php if($whit <> 0){echo"{'name': 'White', 'y': "  . $whit . ", 'color': 'YOURCOLOR'},";} ?>
    
    ]
    }];
    

    【讨论】:

    • 很高兴听到 Erick 的消息。如果对您有帮助,请接受答案:)
    猜你喜欢
    • 1970-01-01
    • 2016-05-14
    • 2020-09-18
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多