Setting Size of Pie Chart in ZingChart - zingchart

I have a visual that includes multiple pie charts (i have a graphset that contains multiple separate pie charts). I want the size of each pie chart to reflect the size of the data in each pie. For example, a pie showing 2010 revenues of $1,000,000 will be smaller than a pie showing 2014 revenues of $2,000,000.
But the size attribute seems to have no impact on the size of each pie, whether I use just a number or a number plus "px".
Does this feature in fact work for pie charts? If so, can someone demonstrate it.

I'm on the ZingChart team, and I'll be happy to help you out with this.
Using the dataparse event, we can retrieve the necessary information about the chart that is to be rendered and make the appropriate calculations and modifications before the render occurs. In this case, we're interested in the values of each pie.
The code snippet below will tally up the totals for each pie chart and generate a percentage modifier. With the current calculations, the largest pie would have a size of 100%, and a pie with exactly half the value of the largest pie would be 50%. You can of course change this if you'd like.
Oh, and if this is too much overkill, you can hard code the size of each pie by setting the "size" attribute in the "plot" object of each chart. You can see this as calculated by my function if you right-click and view the chart source. The values can be percentage values or a numeric value for size in pixels.
Let me know if you have any questions!
// Dataparse is called when the data is ready for the chart
zingchart.dataparse = function (p, d) {
console.log(d);
// Get number of series in graphset, initialize array of 0s
var seriesLength=d.graphset.length;
var total = new Array(seriesLength);
while(seriesLength--) total[seriesLength] = 0;
// Calculate the sum of the values in each series (each pie)
for (var n = 0; n < d.graphset.length; n++) {
for (var m = 0; m < d.graphset[n].series.length; m++) {
total[n] += d.graphset[n].series[m].values[0];
}
}
// Find the largest value in the array of totals
var largest = Math.max.apply(Math, total);
// Calculate % modifier based off of largest value for each pie chart
for (var n=0;n<d.graphset.length;n++){
var sizeModifier=(total[n]/largest)*100;
// Apply the size modifier to the plot object of each pie chart.
d.graphset[n].plot.size = sizeModifier + '%';
}
// Return modified chart object to be rendered.
return d;
}
var oData = {
"layout": "h",
"graphset": [{
"type": "pie",
"plotarea": {
"margin": "0 40"
},
"plot": {
"value-box": {
'visible': 1,
"text":"%v"
}
},
"series": [{
"values": [169]
}, {
"values": [151]
}, {
"values": [142]
}, {
"values": [125]
}]
}, {
"type": "pie",
"plotarea": {
"margin": "0 40"
},
"scaleX": {
},
"scaleY": {
},
"plot": {
"value-box": {
'visible': 1,
"text":"%v"
}
},
"series": [{
"values": [120]
}, {
"values": [89]
}, {
"values": [87]
}, {
"values": [147]
}]
}]
};
zingchart.render({
id: 'chartDiv',
data: oData,
width: 800,
height: 400
});
<script src="http://cdn.zingchart.com/zingchart.min.js"></script>
<div id='chartDiv'></div>

Related

Vertical lines in pdfkit-tables in node.js

I have API that generate pdf file after saving values into database. My customer needed to generate this pdf and then send it by mail. He sended my photo of how should that pdf look like. I recreated it, it looks same as in that picture but it is hard to read because there are missing vertical lines. I looked trought docs and also tried to google, bud I did not found anyithing.
Here is how my PDF looks like:
As you can see, vertical lines are missing and because of that is harder to read.
Is there any possibility to add vertical lines?
Here is my code:
let doc = new PDFDocument({ margin: 30, size: "A4" });
doc.pipe(
fs.createWriteStream(`${problemName}_${creationDate}` + ".pdf")
);
const table = {
title:
"Zápis koordinátora " +
koordinatorName +
" zo dna " +
creationDate +
".",
divider: {
header: { disabled: true },
horizontal: { disabled: false, width: 1, opacity: 1 },
padding: 5,
columnSpacing: 10,
},
headers: [
{ width: 130, renderer: null },
{ width: 130, renderer: null },
{ width: 130, renderer: null },
{ width: 130, renderer: null },
],
rows: [
["Nazov", problemName, "", ""],
[
"Nazov staveniska (Projekt)",
constructionName,
"Na vedomie komu",
"mailing list 1",
],
[
"Vytvoril koordinator BOZP",
koordinatorName,
"Priorita",
problemPriority,
],
["Datum zistenia", creationDate, "Datum odstranenia", ""],
[
"Zodpovedny za vyriesenie zistenia",
"Janko Maly",
"Celkovy pocet zisteni v dni",
10,
],
["Miesto zistenia", discoveryPlace, "Zistenie císlo", 1],
["Popis", problemText],
[
"Navrh na udelenie sankcie",
"50€",
"Pre spolocnost",
adressedFor,
],
],
};
doc.table(table, {
prepareHeader: () => doc.font("Helvetica-Bold").fontSize(8),
prepareRow: (row, indexColumn, indexRow, rectRow, rectCell) => {
doc.font("Helvetica").fontSize(8);
indexColumn === 0;
},
});
doc.end();
I am using pdfkit-table package.
Thank you all
By definition simple PDF structure is not tabular there is one cell (the page) and that one column can be subdivide into two or more rows with null spaces between the text sub columns.
That is why tables are difficult to sub[ex]tract
So adding coloured rows in one area is fairly simple to make like a table, thus to make vertical sub dividers is more difficult, However that feature was added in January 2022 https://github.com/natancabral/pdfkit-table/files/7865078/document-5.pdf
For exsample see https://github.com/natancabral/pdfkit-table/issues/16#issuecomment-1012389097

how to keep the same width in all bars in C3 Javascript library?

I wan't to set all bars the same width, because if I have few bars they get super big, and If I have a lot bars they get very thin.
Here is what I mean:
I have bar width 50 because I tought this was the size of the bar, but it keeps making bars small if the amount increases.
Here is my code:
$(document).ready(function () {
var chart = c3.generate({
bindto: '#stocked',
size: {
height: 320,
width: 1500
},
data: {
labels: ['y',1,2,3,4,5,6],
columns: [
['MUSCLE', <?php echo substr($muscle,1,strlen($muscle)); ?>],
['FAT', <?php echo substr($fat,1,strlen($fat)); ?>]
],
groups: [
['MUSCLE', 'FAT']
],
type: 'bar',
colors: {
MUSCLE: '#75e3ff',
FAT: '#23bbe8'
}
},
bar: {
// width: { ratio: 0.8 }
width: 50,
},
axis: {
x: {
type: 'categories',
show: true,
label: {
text: 'Measurements',
position: 'outer-middle'
},
tick: {
format: "%b",
fit: true,
},
},
y: {
label: {
text: '<?php echo $_SESSION["unit"]; ?>',
position: 'outer-middle'
}
},
},
// subchart: {
// show: true
// },
// zoom: {
// enabled: true
// }
});
});
Thanks and greetings!
The width of your bars is the result of the width of your chart and the amount of data you have populating it. My recommendation would be to size your chart or the container holding your chart accordingly. This will prevent having large white spaces in between each of your bars and result in a more uniform look between your charts.
ex: pseudo code of how to determine how wide you chart should be.
var chart = c3.generate({
size: {
height: 240,
width: function () {
return 6 * 20 + 15
},
data: {
columns: [
['sample', 30, 200, 100, 400, 150, 250]
]
}
});
Here you can see that there of 6 pieces of data and each data is given 20 space and then an extra 15 to account for the axis/labels etc. If each you had 50 data then it should be width: 6 * 20 + 15 ..... of course now you have different width chart but the bar widths and space between bars should be pretty uniform. The more data the longer the chart. You might need to "20" and "15" to get the look you want.

Chart.js - the rightmost data point cut off for line chart

Draw a basic line chart using Chart.js version 2.8.0.
Observe the rightmost data point is cut off. But the leftmost data point (the first data point) looks intact.
I’ve changed the size of the data point circles by changing values of pointRadius. But even in the smallest value, the data point still gets cut off.
I’m not using any plugin or fancy settings. I am filling the chart are with light gray background colour. But other than that, everything else is a box standard Chart.js.
I’ve recreated the issue here:
https://codepen.io/LeoU/pen/gVLybO
And here is my options settings.
options: {
legend: {
display: false,
},
tooltips: {
callbacks: {
label: function (tooltipItem, data) {
var tooltipValue = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
return "£" + parseInt(tooltipValue).toLocaleString();
}
}
},
scales: {
yAxes: [{
gridLines: {
color: "white",
lineWidth: 2
},
ticks: {
beginAtZero: true,
min: 0,
stepSize: 500000,
callback: function(value, index, values) {
return "£" + value.toLocaleString();
},
},
scaleLabel: {
display: true,
labelString: "Median house price"
}
}],
xAxes: [{
display: false,
ticks: {
display: false
}
}]
}
},
Has anyone seen a similar problem with Chart.js?
You can use offset
scales: {
xAxes: [{
offset: true
}]
}
I fixed this by adding padding value to the right hand side of the chart.
I played around with the exact number and 4 looked about right for my chart. But I’d imagine the right number will depend on the size of your pointRadius and other variables you have.
This is what it looks like for me with the changes.
options: {
layout: {
padding: {
left: 0,
right: 4,
top: 0,
bottom: 0
}
}
}
For more about padding for Chart.js, this is their instruction page.
https://www.chartjs.org/docs/latest/configuration/layout.html
It is annoying. And, this should be a default setting, really. But this fixed the problem for me.

How to add titles to the x and y axes in ZingChart

I want to add titles to the x and y axes using ZingChart. For example, if my x-axis shows years (2010, 2011, 2012, etc.), I want the title "Fiscal Year" to be shown below each of the year labels
Similarly, if I am measuring revenues on the y-axis, where I may see labels like 1,000,000, 2,000,000, etc. I want to add a title to the y-axis that says "Annual Revenues".
I couldn't find anything in the documentation about this.
The link Martin used is correct, however there is also documentation about this in the JSON syntax guide. http://www.zingchart.com/docs/json-attributes-syntax/scale-objects/label/
var myChart = {
"graphset":[
{
"type":"mixed",
"background-color":"#ffffff",
"alpha":1,
"title":{
"text":"Browser Usage",
"font-size":"20px",
"font-weight":"bold",
"font-color":"#000000",
"background-color":"#ffffff",
"margin-top":"15px",
"margin-left":"15px",
"margin-bottom":"10px",
"text-align":"left"
},
"scale-x":{
"line-width":"1px",
"line-color":"#CCCCCC",
"line-style":"solid",
"values":["Jan","Feb","Mar","Apr","May","Jun","Jul"],
"guide":{
"line-width":"1px",
"line-color":"#CCCCCC",
"line-style":"solid"
},
"label":{
"text":"Month",
"font-size":"20px",
"color":"red"
},
"item":{
"font-family":"helvetica",
"font-color":"#CCCCCC",
"font-weight":"bold",
"font-size":"10px",
"visible":true
}
},
"scale-y":{
"values":"0:50:10",
"line-width":"1px",
"line-color":"#CCCCCC",
"line-style":"solid",
"tick":{
"visible":true,
"line-color":"#CCCCCC",
"placement":"outer",
"size":"12px"
},
"item":{
"font-color":"#CCCCCC",
"font-weight":"bold",
"font-size":"10px",
"visible":true
},
"guide":{
"line-width":"1px",
"line-color":"#CCCCCC",
"line-style":"solid"
}
},
"tooltip":{
"visible":true
},
"plot":{
"alpha":1,
"hover-state":{
"visible":false
}
},
"series":[
{
"type":"bar",
"values":[47,32,37,48,28,27,32],
"text":"Safari",
"background-color":"#7eac10"
}
]
}
]
};
zingchart.render({
id: "myChart",
height: "300px",
width: "100%",
data: myChart
});
<script src="http://www.zingchart.com/playground/lib/zingchart/zingchart-html5-min.js"></script>
<div id="myChart"></div>
You may have been searching docs for a title. But you can accomplish what you'd like to do by adding a "label" object to your scale-x and scale-y objects and fill it with the attributes necessary to style it for matching your chart.
You can always right click on a chart on ZingChart to see the source.

Dynamic ticks generation flot.js

Why is it that when I put a dynamic array value in the ticks, It always starts with zero but when I specify a min and max in my flot options I get the correct output? any views on this?
I commented the one using min/max:
$(function () {
var data = [ [
[1301634000000, 315.71], //Apr 1, 2011
[1302238800000, 209.21], //Apr 8, 2011
[1302843600000, 420.36], //Apr 15
[1303448400000, 189.86], //4/22
[1304053200000, 314.93], //4/29
[1304658000000, 279.71], //5/6
[1305262800000, 313.34], //5/13
[1305867600000, 114.67], //5/20
[1306472400000, 315.58] //5/27
] ];
var ticks = [];
var ticker=[315.71,209.21,420.36,189.86,314.93,279.71,313.34,114.67,315.58];
for (var i = 0; i < data[0].length; i++) {
ticks.push(data[0][i][0]);
}
$.plot($("#placeholder"), data, {
xaxis: {
mode: "time",
ticks: ticks,
timeformat: "%m/%d",
min: (new Date(2011, 2, 31)).getTime(),
max: (new Date(2011, 4, 31)).getTime()
},
yaxis: {
ticks: ticker
/*min: 114.67,
max: 420.36*/
},
series: {
lines: { show: true },
points: { show: true }
},
grid: {
hoverable: true,
clickable: true
}
});
});
and here is my Fiddle
I notice that in this example, it's not just formatted correctly and not properly scaled so entirely different from what Im asking but I applied the same thing in my prototype.

Resources