如何在Google饼图中连接”% “符号?

huangapple go评论80阅读模式
英文:

How to concat "%" symbol in google pie chart?

问题

我正在尝试在Google饼图的切片中添加"%(百分比)"符号。我已经附上了以下内容。我正在使用Codeigniter框架。以下代码创建了一个Google饼图,当单击其中一个切片时,会弹出另一个饼图。次级饼图显示了"s1"、"s2"、"s3"和"s4"的值。我想在切片的值旁边显示"%(百分比)"符号。请查看以下代码:

index.php文件内的代码

<div id="IR-SLA" style="width: 400px; height: 300px;"></div>
<script>
function updateQuery() {
var month = parseInt(document.getElementById('chooseMonth').value);
var year = document.getElementById('chooseYear').value;
updateIncidentsWithinSLA(month, year);
}
function updateIncidentsWithinSLA(month, year) {
var xhrISLA = new XMLHttpRequest();
xhrISLA.onreadystatechange = function() {
if (xhrISLA.readyState === XMLHttpRequest.DONE) {
if (xhrISLA.status === 200) {

// 从服务器处理响应
var response = xhrISLA.responseText;
var updatedDataISLA = JSON.parse(response);
drawChartISLA(updatedDataISLA);
} else {

// 处理错误情况
console.error('请求失败:记录的图表。');
}
}
};

// 发送异步POST请求以更新查询
xhrISLA.open('POST', '<?php echo base_url(); ?>index.php/IndexController/updateQueryISLA', true);

xhrISLA.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhrISLA.send('month=' + month + '&year=' + year);
}

<script type="text/javascript">
google.charts.load('current', {
'packages': ['corechart']
});

function drawChartISLA(updatedDataISLA) {
// 创建数据表
var data = new google.visualization.DataTable();
data.addColumn('string', 'opco_name');
data.addColumn('number', 'count');
data.addColumn('number', 's1_sla_met_count');
data.addColumn('number', 's2_sla_met_count');
data.addColumn('number', 's3_sla_met_count');
data.addColumn('number', 's4_sla_met_count');
data.addRows(updatedDataISLA);
// 设置图表选项
var options = {
'title': '事件SLA达标百分比 - ',
'pieSliceText': 'value',
is3D: 'true',
'tooltip': {
trigger: 'none'
}
};

// 实例化并绘制图表
var chart = new google.visualization.PieChart(document.getElementById('IR-SLA'));
chart.draw(data, options);
// 为切片选择添加事件侦听器
google.visualization.events.addListener(chart, 'select', selectHandler);
// 切片选择处理程序
function selectHandler() {
// 获取选定的切片数据
var selection = chart.getSelection()[0];
if (selection) {
var sliceName = data.getValue(selection.row, 0);
var sliceCount = data.getValue(selection.row, 1);
var sliceS1 = data.getValue(selection.row, 2);
var sliceS2 = data.getValue(selection.row, 3);
var sliceS3 = data.getValue(selection.row, 4);
var sliceS4 = data.getValue(selection.row, 5);
// 为选定的切片创建数据表
var sliceData = new google.visualization.DataTable();
sliceData.addColumn('string', 'Severity');
sliceData.addColumn('number', 'Incident Count');
sliceData.addRow(['S1', sliceS1]);
sliceData.addRow(['S2', sliceS2]);
sliceData.addRow(['S3', sliceS3]);
sliceData.addRow(['S4', sliceS4]);
// 设置选定切片的图表选项
var sliceOptions = {
'title': sliceName + ' SLA达标百分比 - ',
pieSliceText: 'value',
'width': 500,
'height': 300,
is3D: 'true',
'tooltip': {
'text': 'value'
}
};
// 打开带有切片数据的弹出窗口
var popup = window.open('', 'myPopup', 'width=600,height=400');
popup.document.write('<div style="margin:auto" id="slice_chart_div"></div>');
centerPopup(popup);
// 实例化并绘制选定切片的图表
var sliceChart = new google.visualization.PieChart(popup.document.getElementById('slice_chart_div'));
sliceChart.draw(sliceData, sliceOptions);
}
}

function centerPopup(popup) {
var left = (screen.width - popup.outerWidth) / 2;
var top = (screen.height - popup.outerHeight) / 2;
popup.moveTo(left, top);
}
}
</script>

"updateQuery()"在按钮点击时调用。

以下代码在"IndexController.php"文件中

public function updateQueryISLA()
{
// 获取发布的月份和年份值
$selected_month = $this->input->post('month') ? $this->input->post('month') : date('n');
$selected_year = $this->input->post('year') ? $this->input->post('year') : date('Y');
// 使用新参数更新查询
$query = $this->db->query("SELECT o.opco_name, s1_incidents_count, s2_incidents_count, 
s3_incidents_count, s4_incidents_count, ((i.s1_sla_met_count + i.s2_sla_met_count + 
i.s3_sla_met_count + i.s4_sla_met_count)/(s1_incidents_count + 
s2_incidents_count+s3_incidents_count+s4_incidents_count))* 100 as count, 
(i.s1_sla_met_count/s1_incidents_count)*100 as s1, 
(i.s2_sla_met_count/s2_incidents_count)*100 as s2, 
(i.s3_sla_met_count/s3_incidents_count)*100 as s3, 
(i.s4_sla_met_count/s4_incidents_count)*100 as s4 FROM opcos_list o inner JOIN 
incidents_summary i ON o.id = i.opcos_list_id WHERE i.month_number = $selected_month AND 
i.year = $selected_year");
$rows = $query->result_array();

// 为响应准备数据数组
$data = [];
foreach ($rows as $row) {
$data[] = [
$row['opco_name'],
(int)$row['count'],
(int)$row['s1'],
(int)$row['s2'],
(int)$row['s3'],
(int)$row['s4'],
];
}

// 将更新后的查询结果返回为JSON
echo json_encode($data);
}

我尝试使用concatenate函数在查询中添加了"%"符号。该查询在MySQL Workbench中有效,但未显示在饼图上。请为我提供一个解决方案。

英文:

I am trying to add the "%" symbol in the Google pie chart slices. I have attached the below. I am using the Codeigniter framework. The below code creates a Google pie chart which pops up another pie chart when clicked on one of the slices. The secondary pie chart shows the values of "s1", "s2", "s3", and "s4". I want to display the "%" symbol beside the values of the slices. Please find the code below:

The code inside index.php

&lt;div id=&quot;IR-SLA&quot; style=&quot;width: 400px; height: 300px;&quot;&gt;&lt;/div&gt;
&lt;script&gt;
function updateQuery() {
var month = parseInt(document.getElementById(&#39;chooseMonth&#39;).value);
var year = document.getElementById(&#39;chooseYear&#39;).value;
updateIncidentsWithinSLA(month, year);
}
function updateIncidentsWithinSLA(month, year) {
var xhrISLA = new XMLHttpRequest();
xhrISLA.onreadystatechange = function() {
if (xhrISLA.readyState === XMLHttpRequest.DONE) {
if (xhrISLA.status === 200) {
// Handle the response from the server
var response = xhrISLA.responseText;
var updatedDataISLA = JSON.parse(response);
drawChartISLA(updatedDataISLA);
} else {
// Handle the error case
console.error(&#39;Request failed for incidents logged chart.&#39;);
}
}
};
// Send an asynchronous POST request to update the query
xhrISLA.open(&#39;POST&#39;, &#39;&lt;?php echo base_url(); ? 
&gt;index.php/IndexController/updateQueryISLA&#39;, true);
xhrISLA.setRequestHeader(&#39;Content-Type&#39;, &#39;application/x-www-form-urlencoded&#39;);
xhrISLA.send(&#39;month=&#39; + month + &#39;&amp;year=&#39; + year);
}
&lt;script type=&quot;text/javascript&quot;&gt;
google.charts.load(&#39;current&#39;, {
&#39;packages&#39;: [&#39;corechart&#39;]
});
function drawChartISLA(updatedDataISLA) {
// Create the data table
var data = new google.visualization.DataTable();
data.addColumn(&#39;string&#39;, &#39;opco_name&#39;);
data.addColumn(&#39;number&#39;, &#39;count&#39;);
data.addColumn(&#39;number&#39;, &#39;s1_sla_met_count&#39;);
data.addColumn(&#39;number&#39;, &#39;s2_sla_met_count&#39;);
data.addColumn(&#39;number&#39;, &#39;s3_sla_met_count&#39;);
data.addColumn(&#39;number&#39;, &#39;s4_sla_met_count&#39;);
data.addRows(updatedDataISLA);
// Set chart options
var options = {
&#39;title&#39;: &#39;Incidents SLA met percentage - &#39;,
&#39;pieSliceText&#39;: &#39;value&#39;,
is3D: &#39;true&#39;,
&#39;tooltip&#39;: {
trigger: &#39;none&#39;
}
};
// Instantiate and draw the chart
var chart = new google.visualization.PieChart(document.getElementById(&#39;IR-SLA&#39;));
chart.draw(data, options);
// Add event listener for slice selection
google.visualization.events.addListener(chart, &#39;select&#39;, selectHandler);
// Slice selection handler
function selectHandler() {
// Get selected slice data
var selection = chart.getSelection()[0];
if (selection) {
var sliceName = data.getValue(selection.row, 0);
var sliceCount = data.getValue(selection.row, 1);
var sliceS1 = data.getValue(selection.row, 2);
var sliceS2 = data.getValue(selection.row, 3);
var sliceS3 = data.getValue(selection.row, 4);
var sliceS4 = data.getValue(selection.row, 5);
// Create data table for selected slice
var sliceData = new google.visualization.DataTable();
sliceData.addColumn(&#39;string&#39;, &#39;Severity&#39;);
sliceData.addColumn(&#39;number&#39;, &#39;Incident Count&#39;);
sliceData.addRow([&#39;S1&#39;, sliceS1]);
sliceData.addRow([&#39;S2&#39;, sliceS2]);
sliceData.addRow([&#39;S3&#39;, sliceS3]);
sliceData.addRow([&#39;S4&#39;, sliceS4]);
// Set chart options for selected slice
var sliceOptions = {
&#39;title&#39;: sliceName + &#39; SLA met percentage - &#39;,
pieSliceText: &#39;value&#39;,
&#39;width&#39;: 500,
&#39;height&#39;: 300,
is3D: &#39;true&#39;,
&#39;tooltip&#39;: {
&#39;text&#39;: &#39;value&#39;
}
};
// Open popup window with slice data
var popup = window.open(&#39;&#39;, &#39;myPopup&#39;, &#39;width=600,height=400&#39;);
popup.document.write(&#39;&lt;div style=&quot;margin:auto&quot; id=&quot;slice_chart_div&quot;&gt;&lt;/div&gt;&#39;);
centerPopup(popup);
// Instantiate and draw the chart for selected slice
var sliceChart = new 
google.visualization.PieChart(popup.document.getElementById(&#39;slice_chart_div&#39;));
sliceChart.draw(sliceData, sliceOptions);
}
}
function centerPopup(popup) {
var left = (screen.width - popup.outerWidth) / 2;
var top = (screen.height - popup.outerHeight) / 2;
popup.moveTo(left, top);
}
}
&lt;/script&gt;

The "updateQuery()" is called when a button is clicked.

The below code is in the "IndexController.php" file

public function updateQueryISLA()
{
// Get the posted month and year values
$selected_month = $this-&gt;input-&gt;post(&#39;month&#39;) ? $this-&gt;input-&gt;post(&#39;month&#39;) : date(&#39;n&#39;);
$selected_year = $this-&gt;input-&gt;post(&#39;year&#39;) ? $this-&gt;input-&gt;post(&#39;year&#39;) : date(&#39;Y&#39;);
// Update the query with the new parameters
$query = $this-&gt;db-&gt;query(&quot;SELECT o.opco_name, s1_incidents_count, s2_incidents_count, 
s3_incidents_count, s4_incidents_count, ((i.s1_sla_met_count + i.s2_sla_met_count + 
i.s3_sla_met_count + i.s4_sla_met_count)/(s1_incidents_count + 
s2_incidents_count+s3_incidents_count+s4_incidents_count))* 100 as count, 
(i.s1_sla_met_count/s1_incidents_count)*100 as s1, 
(i.s2_sla_met_count/s2_incidents_count)*100 as s2, 
(i.s3_sla_met_count/s3_incidents_count)*100 as s3, 
(i.s4_sla_met_count/s4_incidents_count)*100 as s4 FROM opcos_list o inner JOIN 
incidents_summary i ON o.id = i.opcos_list_id WHERE i.month_number = $selected_month AND 
i.year = $selected_year&quot;);
$rows = $query-&gt;result_array();
// Prepare the data array for the response
$data = [];
foreach ($rows as $row) {
$data[] = [
$row[&#39;opco_name&#39;],
(int)$row[&#39;count&#39;],
(int)$row[&#39;s1&#39;],
(int)$row[&#39;s2&#39;],
(int)$row[&#39;s3&#39;],
(int)$row[&#39;s4&#39;],
];
}
// Return the updated query result as JSON
echo json_encode($data);
}

I tried to add "%" in the query using the concate function. The query worked in Mysql workbench but did not appear on the pie charts.
Please provide me a solution.

答案1

得分: 1

要在饼图上显示的值后添加百分号(%),需要相应地格式化数据表中的数字。

为此,您可以使用谷歌的 NumberFormat 类。

在创建饼图的数据表之后...

// 为所选切片创建数据表
var sliceData = new google.visualization.DataTable();
sliceData.addColumn('string', '严重程度');
sliceData.addColumn('number', '事件计数');
sliceData.addRow(['S1', sliceS1]);
sliceData.addRow(['S2', sliceS2]);
sliceData.addRow(['S3', sliceS3]);
sliceData.addRow(['S4', sliceS4]);

使用谷歌的 NumberFormat 类创建一个数字格式...

var percentFormat = new google.visualization.NumberFormat({
  fractionDigits: 0,  // 要显示的小数位数
  suffix: '%'         // 数字后要显示的文本
});

然后使用它来格式化第二列的数字(索引为1)...

percentFormat.format(sliceData, 1);

确保在绘制图表之前按照上述方式格式化值。

注意:这也会在悬停在切片上时格式化工具提示中显示的数字。

英文:

to add the percent symbol (%) after the values displayed on the pie slices,
you need to format the numbers in the data table accordingly.

to do so, you can use google's NumberFormat class.

after you create the data table for the pie chart...

// Create data table for selected slice
var sliceData = new google.visualization.DataTable();
sliceData.addColumn(&#39;string&#39;, &#39;Severity&#39;);
sliceData.addColumn(&#39;number&#39;, &#39;Incident Count&#39;);
sliceData.addRow([&#39;S1&#39;, sliceS1]);
sliceData.addRow([&#39;S2&#39;, sliceS2]);
sliceData.addRow([&#39;S3&#39;, sliceS3]);
sliceData.addRow([&#39;S4&#39;, sliceS4]);

create a number format using google's NumberFormat class...

var percentFormat = new google.visualization.NumberFormat({
fractionDigits: 0,  // number of decimal places to display
suffix: &#39;%&#39;         // text to display after the number
});

then use it to format the numbers in the second column (index = 1)...

percentFormat.format(sliceData, 1);

be sure to format the values as shown above before drawing the chart.

note: this will also format the number shown in the tooltip when hovering a slice.

huangapple
  • 本文由 发表于 2023年6月5日 20:48:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76406569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定