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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料