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

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

报告相同问题?

悬赏问题

  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成