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

Javascript 在amcharts中为图例添加另一个字段以显示百分比,javascript,php,legend,amcharts,Javascript,Php,Legend,Amcharts,我正在使用amcharts为codeigniter中的一些细节创建一个饼图,我使用带有图例()的饼图,并使您可以像在演示中一样查看带有标题和值的图例。现在,我想为图例添加另一个字段,该字段在每行中显示相关百分比。有什么关键字或方法可以做到这一点吗?我是编程新手。 这是我的密码 $data['chartOne'] = $this->prepareJson($totalQuotations, "q"); $data['chartTwo'] = $this->prepareJson($to

我正在使用amcharts为codeigniter中的一些细节创建一个饼图,我使用带有图例()的饼图,并使您可以像在演示中一样查看带有标题和值的图例。现在,我想为图例添加另一个字段,该字段在每行中显示相关百分比。有什么关键字或方法可以做到这一点吗?我是编程新手。 这是我的密码

$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;
}
$data['chartOne']=$this->prepareJson($totalquotes,“q”);
$data['chartTwo']=$this->prepareJson($totalContribution,“c”);
var chart1=AmCharts.makeChart(“salesReportPerformanceChartdiv”,”.$data[“chartOne”]。);
var chart2=AmCharts.makeChart(“salesReportContributionChartdiv,”.$data[“chartTwo”]”);
';
公共函数prepareJson($data,$type){
$chatData=[];
foreach($数据作为$状态){
$stustotal=new\stdClass();
$stustotal->y=$status->count;
如果($type==“q”){
$stustotal->x=$status->status\u name;
}else if($type==“c”){
$stustotal->x=$status->user\u name;
}
阵列推送($chatData,$stustotal);
}
$listeners=new\stdClass();
$listeners->method=addLegendLabel;
$listeners->event=“draw”;
$export=new\stdClass();
$export->enabled=true;
$legend=new\stdClass();
$legend->position=“right”;
$legend->markerType=“圆圈”;
$legend->autoMargins=true;
$feOffset=new\stdClass();
$feOffset->result=“offOut”;
$feOffset->in=“SourceAlpha”;
$feOffset->dx=0;
$feOffset->dy=0;
$feGaussianBlur=new\stdClass();
$feGaussianBlur->result=“脱口而出”;
$feGaussianBlur->in=“offOut”;
$feGaussianBlur->stdDeviation=5;
$feBlend=new\stdClass();
$feBlend->in=“SourceGraphic”;
$feBlend->in2=“脱口而出”;
$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=(数组)$chatData;
$chart->valueField=“y”;
$chart->titleField=“x”;
$chart->labelRadius=5;
$chart->radius=“42%”;
$chart->labelText=“”;
$chart->listeners=$listeners;
$chart->export=$export;
$chartJSON=json_编码($chart);
返回$chartJSON;
}

您可以通过在字符串中添加
[[percents]]
快捷码来自定义图例以包含百分比。默认情况下,它设置为
“[[value]]”
。在您的情况下,似乎必须通过
$legend
变量进行设置。您可能还希望设置以适应较长的值字符串,如下所示:

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