dpvv37755 2017-11-13 10:11
浏览 179
已采纳

在amcharts中为图例添加另一个字段以显示百分比

I am using amcharts to create a piechart for some details in codeigniter and I use pie chart with legend(http://www.amcharts.com/demos/pie-chart-with-legend/) and I made view the legend with title and value just like in the demo. Now I want to add another field for the legend that display relevant percentage in line for each. Are there any keyword or method to do that? I am new to programming. Here's my code..

$data['chartOne'] = $this->prepareJson($totalQuotations, "q");
$data['chartTwo'] = $this->prepareJson($totalContribution, "c");

<script type = "text/javascript">
                    var chart1 = AmCharts.makeChart( "salesReportPerformanceChartdiv",' . $data["chartOne"] . ');
                    var chart2 = AmCharts.makeChart( "salesReportContributionChartdiv",' . $data["chartTwo"] . ');
                </script>';


public function prepareJson($data, $type) {
    $chatData = [];
    foreach ($data as $status) {
        $stustotal = new \stdClass();
        $stustotal->y = $status->count;
        if ($type == "q") {
            $stustotal->x = $status->status_name;
        } else if ($type == "c") {
            $stustotal->x = $status->user_name;
        }
        array_push($chatData, $stustotal);
    }

    $listeners = new \stdClass();
    $listeners->method = addLegendLabel;
    $listeners->event = "drawn";

    $export = new \stdClass();
    $export->enabled = true;

    $legend = new \stdClass();
    $legend->position = "right";
    $legend->markerType = "circle";
    $legend->autoMargins = true;

    $feOffset = new \stdClass();
    $feOffset->result = "offOut";
    $feOffset->in = "SourceAlpha";
    $feOffset->dx = 0;
    $feOffset->dy = 0;

    $feGaussianBlur = new \stdClass();
    $feGaussianBlur->result = "blurOut";
    $feGaussianBlur->in = "offOut";
    $feGaussianBlur->stdDeviation = 5;

    $feBlend = new \stdClass();
    $feBlend->in = "SourceGraphic";
    $feBlend->in2 = "blurOut";
    $feBlend->mode = "normal";

    $filter = new \stdClass();
    $filter->id = "shadow";
    $filter->width = "200%";
    $filter->height = "200%";
    $filter->feOffset = $feOffset;
    $filter->feGaussianBlur = $feGaussianBlur;
    $filter->feBlend = $feBlend;

    $defs = new \stdClass();
    $defs->filter = $filter;

    $chart = new \stdClass();
    $chart->type = "pie";
    $chart->startDuration = 0;
    $chart->theme = "light";
    $chart->addClassNames = true;
    $chart->legend = $legend;
    $chart->innerRadius = "85%";
    $chart->defs = $defs;
    $chart->dataProvider = (array)$chatData;
    $chart->valueField = "y";
    $chart->titleField = "x";
    $chart->labelRadius = 5;
    $chart->radius = "42%";
    $chart->labelText = "";
    $chart->listeners = $listeners;
    $chart->export = $export;

    $chartJSON = json_encode($chart);

    return $chartJSON;
}
  • 写回答

1条回答 默认 最新

  • duan0427 2017-11-13 12:33
    关注

    You can customize the legend's valueText to include the percents by adding the [[percents]] shortcode to the string. By default, it is set to "[[value]]". In your case, it looks like you have to set it through your $legend variable. You might also want to set the valueWidth to accommodate for the longer value string like so:

    $legend->valueText = "[[value]] [[percents]]%"; //customize as needed
    $legend->valueWidth = 100; //adjust as needed so it doesn't overlap
    

    Fiddle

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵