Generate multiple charts based on json in angular 8 - highcharts

I am using high charts in angular 8.I am getting chart for single object in an array of objects but i want to get each chart for each object dynamically how to achieve that. Below I have attached my working stackblitz for reference.
JSON:
[
{
"nameList":[
{
"id":203,
"Namelist_constrains":{
},
"lists":[
{
"name":"books",
"data":{
"A15people":4285,
"A30people":4285,
"A90people":4285
}
},
{
"name":"paper",
"data":{
"A15people":4285,
"A30people":4285,
"A90people":4285
}
},
{
"name":"pen",
"data":{
"A15people":4285,
"A30people":4285,
"A90people":4285
}
},
{
"name":"ball",
"data":{
"A15people":4285,
"A30people":4285,
"A90people":4285
}
},
{
"name":"bat",
"data":{
"A15people":4285,
"A30people":4285,
"A90people":4285
}
}
]
},
{
"id":204,
"Namelist_constrains":{
},
"lists":[
{
"name":"books",
"data":{
"A15people":5004,
"A30people":345,
"A90people":1334
}
},
{
"name":"paper",
"data":{
"A15people":112,
"A30people":345,
"A90people":6667
}
},
{
"name":"pen",
"data":{
"A15people":9882,
"A30people":3456,
"A90people":29898
}
},
{
"name":"ball",
"data":{
"A15people":3465,
"A30people":224,
"A90people":455
}
},
{
"name":"bat",
"data":{
"A15people":876,
"A30people":234,
"A90people":664
}
}
]
}
]
}
]
For the above I need to get two charts because it have two objects.
In my stackblitz i have only one object in the json. I am little bit confused how to achieve this.help me to solve this.
Link:
https://stackblitz.com/edit/angular-highcharts-example-xqcnyv?file=src/app/app.component.ts

You could create a simple component that will take a chart's data as it's Input and return standalone chart. Then you might use the *ngFor directive to generate that component for each element of your data.
Unfortunately, I am not familiar with the angular-highcharts, so I created the simple demo with highcharts-angular - the officially supported Angular wrapper.
Live demo:
https://stackblitz.com/edit/highcharts-angular-sparkline-j3nujf?file=src%2Fapp%2Fapp.component.html
data:
myData = [
{ data: [1, 2, 3], name: "first" },
{ data: [4, 5, 6], name: "second" },
{ data: [7, 8, 9], name: "third" }
];
app.component.html
<app-spark-line
*ngFor="let elem of myData"
[name]="elem.name"
[data]="elem.data"
></app-spark-line>
spark-line.component.ts:
export class SparkLineComponent {
Highcharts: typeof Highcharts = Highcharts;
#Input() data: Array<number>;
#Input() name: string;
updateFlag = false;
chartOptions: Options = {
chart: {
type: "area",
margin: [2, 0, 2, 0],
width: 200,
height: 200,
},
series: [
{
name: '',
type: 'area',
data: []
}
]
};
ngOnChanges(change: SimpleChanges) {
this.chartOptions.series = [{
name: change.name ? change.name.currentValue : null,
type: 'area',
data: change.data.currentValue,
}];
this.updateFlag = true;
}
}
sparkline.component.html:
<highcharts-chart
[Highcharts]="Highcharts"
[options]="chartOptions"
[(update)]="updateFlag"
>
</highcharts-chart>

Related

Highcharts - Highstock chart showing double line on hovering data points to show tooltip

Highstock chart showing double line on hovering data points to show the tooltip, which supose to be single and should be in sync.
AS one can check in foddle (link shared below and in snashot) the line on hovering are double and somewhat not in sync. In series second object(2end series), if we set xaxis as 0 or remove it then this issue disappears but xaxis labels also gets removed. I need both the xaxis labels and single line on hover.
I am sharing the link to the fiddle:
https://jsfiddle.net/akt6284m/2/
code:
Highcharts.stockChart('container', {
series: [
{
"type":"areaspline",
"color":"rgba(87,169,222,0.8)",
"data":[
{
"y":0,
"x":1608737040000,
},
{
"y":0,
"x":1608737100000,
},
{
"y":2,
"x":1608737160000,
},
{
"y":0,
"x":1608737220000,
},
{
"y":0,
"x":1608737280000,
},
{
"y":0,
"x":1608737340000,
},
{
"y":0,
"x":1608737400000,
},
....
]
},
{
"type":"column",
"groupPadding":0,
"showInLegend":false,
"yAxis":1,
"xAxis":1,
"data":[
{
"y":1,
"x":1608737040000,
"color":"#02DE27",
},
{
"y":1,
"x":1608737100000,
"color":"#02DE27",
},
{
"y":1,
"x":1608737160000,
"color":"#FF3635",
},
{
"y":1,
"x":1608737220000,
"color":"#02DE27",
},
{
"y":1,
"x":1608737280000,
"color":"#02DE27",
},
{
"y":1,
"x":1608737340000,
"color":"#02DE27",
},
{
"y":1,
"x":1608737400000,
"color":"#02DE27",
},
{
"y":1,
"x":1608737460000,
"color":"#02DE27",
},
....
],
"label":{
"format":""
}
},
{
"type":"scatter",
"data":[
[
1608737160000,
0
],
[
1608738060000,
0
]
],
"color":"#FF3635"
}
],
yAxis: [{
height: '50%'
}, {
height: '50%',
top: '50%'
}],
xAxis: [{
tickLength: 0,
lineWidth: 1,
labels: {
enabled: false
},
height: '50%'
}, {
top: '50%',
height: '50%',
}]
});
Please if anyone can help me with this issue.
The column series has different padding for the xAxis by default. Try to use this config for the xAxis which is assigned to the line series.
xAxis: [{
minPadding: 0.021,
maxPadding: 0.021,
labels: {
enabled: false
},
height: '50%'
}, {
top: '50%',
height: '50%',
}]
Demo: https://jsfiddle.net/BlackLabel/eu94vcta/
API: https://api.highcharts.com/highcharts/xAxis.maxPadding

Getting Unsuccessful webhook call: Failed to translate JSON to ExecuteHttpResponse for table in #assistant/conversation

I am trying to show a table in my cloud function using the new #assistant/conversation with the array of data but when i test the action i am getting the error as below
Unsuccessful webhook call: Failed to translate JSON to ExecuteHttpResponse
But when i check the logs i am getting the row values like below
{
"responseJson": {
"session": {
"id": "ABwppHE5M8EGlWf3YmpUUGPQ5xxHh-cb2QYyF_YUarZbF_jXq-Ad2iKDtyI8XAyvWPp4hHnQockBWMZuQA",
"params": {},
"languageCode": ""
},
"prompt": {
"override": false,
"content": {
"table": {
"button": {},
"columns": [
"Date",
"Time",
"Place"
],
"image": {},
"rows": [
"20-10-2020",
"11:20",
"Test"
],
"subtitle": "",
"title": ""
}
}
}
}
}
Here is the implementation of my adding table in the conv
const tempDatas = ['20-10-2020', '11:20', 'Test'];
conv.add(
new Table({
dividers: true,
columns: ['Date', 'Time', 'Place'],
rows: tempDatas
})
);
I have used the same logic in google-actions plugin there it works fine.I have imported the Table like below
const { conversation, Table } = require('#assistant/conversation');
Fixed the issue. The structure provided by the new actions builder is little different with the old one
New Structure:
conv.add(new Table({
'title': 'Table Title',
'subtitle': 'Table Subtitle',
'image': ASSISTANT_LOGO_IMAGE,
'columns': [{
'header': 'Column A',
}, {
'header': 'Column B',
}, {
'header': 'Column C',
}],
'rows': [{
'cells': [{
'text': 'A1',
}, {
'text': 'B1',
}, {
'text': 'C1',
}],
}, {
'cells': [{
'text': 'A2',
}, {
'text': 'B2',
}, {
'text': 'C2',
}],
}, {
'cells': [{
'text': 'A3',
}, {
'text': 'B3',
}, {
'text': 'C3',
}],
}],
}));
You can simplify it like below that is the solution for my above issue
const tableRows = [];
tablesRows.push({ 'cells': [{ 'text': moment(data.date).format('DD-MM-YYYY') }, { 'text': data.time }, { 'text': data.place }] });
conv.add(new Table({
'columns': [{
'header': 'Date',
}, {
'header': 'Time',
}, {
'header': 'Place',
}],
'rows': tablesRows,
}));
For more info visit conversation components

Highcharts stacked columns based on each day value

i'm trying to make a stacked column chart based on the following values
[
{
"date": "2019-06-08",
"profileId": "2978",
"likes": "48",
"source": "text",
"brand": "Pepsi",
"comments": "8",
"followers": "23443",
"ER": "1.00439"
},
{
"date": "2019-06-10",
"profileId": "6456",
"likes": "93",
"source": "media",
"brand": "pepsi",
"comments": "3",
"followers": "54482",
"ER": "0.99969"
},
{
"date": "2019-06-10",
"profileId": "6215",
"likes": "457",
"source": "media",
"brand": "pepsi",
"comments": "11",
"followers": "233113",
"ER": "1.00818"
}
]
and here's the chart object
const brandsValueChartOptions = {
chart: {
type: 'column',
}
credits: {
enabled: false,
},
xAxis: {
title: {
text: 'Date',
},
categories: brandsValue.x,
labels: {
formatter: function() {
return moment(this.value).format('Do MMM')
},
},
crosshair: true,
grouping: true
},
yAxis: {
min: 0,
title: {
text: 'Value (in USD)',
},
},
tooltip: {
headerFormat: '<span style="font-size:10px">{point.key}</span>',
},
plotOptions: {
column: {
pointPadding: 0.2,
borderWidth: 0,
stacking: 'normal',
},
},
series: {
stacking: 'true'
}
},
series: brandsValue.y,
}
and here how i process the data on the structure the results object to be used in the chart object above.
const brandsValue = {
x: [],
y: [],
}
let colorIndex = 0
_.chain(data)
.map(brand => {
return {
...brand,
day: brand.date,
brand: _.capitalize(brand.brand.toLowerCase()),
}
})
.forEach(brand => brandsValue.x.push(brand.day))
.groupBy(brand => brand.brand)
.forEach(items => {
if (values.mediaTypeSelected === 'posts') {
items.forEach(item => {
let followersCount = parseInt(item.followers)
let er = parseFloat(item.ER)
item.finalCost = 100 * er
item.date = new Date(item.date).getTime()
return item
})
}
})
.forEach(item => {
let sorted = _.sortBy(item, i => moment(i.date))
let values = {
name: item[0].brand,
color: "#cccccc",
data: [],
}
sorted.forEach(i => {
if (this.state.mediaTypeSelected === 'posts') {
values.data.push([
moment(i.date).format('Do MMM'),
parseInt(i.finalCost),
i.profileId,
])
}
})
brandsValue.y.push(values)
})
.value()
The problem here, is that the x-axis values aren't grouped/stacked based on the date, i've tried several solutions with no luck,
here's how the chart results looks like

highcharts-export-server yAxis formatter does not work

highcharts can set yAxis formatter function to change yAxis format,but
using highcharts-export-server to generate image, yAxis formatter does not work.
the demo code is like this:
const exporter = require("highcharts-export-server");
const fs = require("fs");
const options = {
"xAxis": {
"categories": ['1', '2', '3', '4']
},
"yAxis": {
title: {
text: 'times'
},
plotLines: [
{
value: 0,
width: 1,
color: "#808080"
}
],
labels: {
enabled: true,
formatter: function () {
return this.value * 100
},
},
},
"series": [{
"data": [1, 3, 2, 4],
"type": "line"
}, {
"data": [5, 3, 4, 2],
"type": "line"
}]
}
const exportSettings = {type: 'png',options}
//Set up a pool of PhantomJS workers
exporter.initPool();
//Perform an export
exporter.export(exportSettings, function (err, res) {
var dataBuffer = new Buffer(res.data, 'base64');
fs.writeFile("out.png", dataBuffer, function (err) {
if (err) {
console.log(err);
} else {
console.log("save success!");
}
});
exporter.killPool();
});
any one have some suggestions?thanks!
the result:
demo result
There's a bug associated with this problem: https://github.com/highcharts/node-export-server/issues/70
However you can use the formatter function in callback export parameter like this:
{
"type": "Png",
callback: `function(chart) {
chart.yAxis[0].update({
labels: {
enabled: true,
formatter: function() {
return this.value * 100
}
}
});
}`,
options: {
"xAxis": {
"categories": ['1', '2', '3', '4', '5']
(...)
Notice that the whole function is a String.

Highcharts async multi level drill down

I am looking for help in implementing multi-level drill down with Highcharts async drill down. I was able to implement the drill down to first level but i need drill down to one more level. Below is sample example which i am using for reference. In below example i can drill down to "Animals","Fruits" & "Cars". I also can click on Sheep and able to drilldown but how can i add series which will be displayed after i click on Sheep. Thanks for any help..
$('#container').highcharts({
chart: {
type: 'column',
events: {
drilldown: function (e) {
if (!e.seriesOptions) {
var chart = this,
drilldowns = {
'Animals': {
name: 'Animals',
data: [
{name: 'Sheep',
y:5,
drilldown: true
},
['Cow', 3]
]
},
'Fruits': {
name: 'Fruits',
data: [
['Apples', 5],
['Oranges', 7],
['Bananas', 2]
]
},
'Cars': {
name: 'Cars',
data: [
['Toyota', 1],
['Volkswagen', 2],
['Opel', 5]
]
}
},
series = drilldowns[e.point.name];
// Show the loading label
chart.showLoading('Simulating Ajax ...');
setTimeout(function () {
chart.hideLoading();
chart.addSeriesAsDrilldown(e.point, series);
}, 1000);
}
}
}
},
title: {
text: 'Async drilldown'
},
xAxis: {
type: 'category'
},
legend: {
enabled: false
},
plotOptions: {
series: {
borderWidth: 0,
dataLabels: {
enabled: true
}
}
},
series: [{
name: 'Things',
colorByPoint: true,
data: [{
name: 'Animals',
y: 5,
drilldown: true
}, {
name: 'Fruits',
y: 2,
drilldown: true
}, {
name: 'Cars',
y: 4,
drilldown: true
}]
}],
drilldown: {
series: []
}
});
});
Since their sample code references the point's name, should should create the drilled down values in the "drilldowns" dictionary using the data's parent name as the dictionary key. Here is an example of drilling into Dogs:
if (!e.seriesOptions) {
var chart = this,
drilldowns = {
'Animals': {
name: 'Animals',
data: [
{name: 'Dogs',
drilldown: true,
y: 2
},
['Cows', 2],
['Sheep', 3]
]
},
'Fruits': {
name: 'Fruits',
data: [
['Apples', 5],
['Oranges', 7],
['Bananas', 2]
]
},
'Cars': {
name: 'Cars',
data: [
['Toyota', 1],
['Volkswagen', 2],
['Opel', 5]
]
},
"Dogs": {
name: 'Dogs',
data: [
{
name: 'Rottweiler',
drilldown: true,
y: 6,
}, {
name: 'Pit Bull',
y: 2,
}, {
name: 'Poodle',
y: 4,
}]
},
"Rottweiler": {
name: 'Rottweiler',
data: [
['male', 4],
['female', 2]
]
}
},
series = drilldowns[e.point.name];
// Show the loading label
chart.showLoading('Simulating Ajax ...');
setTimeout(function () {
chart.hideLoading();
chart.addSeriesAsDrilldown(e.point, series);
}, 200);
}
Notice I added two new keys: "Dogs" and "Rottweiler".
Hope that helps.
$(function () {
// Create the chart
$('#container').highcharts({
chart: {
type: 'column',
events: {
drilldown: function (e) {
if (!e.seriesOptions) {
var chart = this,
drilldowns = {
'Animals': {
name: 'Animals',
data: [
['Cows', 2, true],
['Sheep', 3, true]
],
keys: ['name','y','drilldown']
},
'Sheep': {
name: 'Sheep',
data: [
['Red', 2],
['White', 1]
]
},
'Cows': {
name: 'Cows',
data: [
['Red', 1],
['White', 1]
]
},
'Fruits': {
name: 'Fruits',
data: [
['Apples', 5, true],
['Oranges', 7, true],
['Bananas', 2]
],
keys: ['name','y','drilldown']
},
'Apples': {
name: 'Apples',
data: [
['Red', 3],
['Green', 2]
]
},
'Cars': {
name: 'Cars',
data: [
['Toyota', 1],
['Volkswagen', 2],
['Opel', 5]
]
}
},
series = drilldowns[e.point.name];
// Show the loading label
chart.showLoading('Simulating Ajax ...');
setTimeout(function () {
chart.hideLoading();
chart.addSeriesAsDrilldown(e.point, series);
}, 1000);
}
}
}
},
title: {
text: 'Async drilldown'
},
xAxis: {
type: 'category'
},
legend: {
enabled: false
},
plotOptions: {
series: {
borderWidth: 0,
dataLabels: {
enabled: true
}
}
},
series: [{
name: 'Things',
colorByPoint: true,
data: [{
name: 'Animals',
y: 5,
drilldown: true
}, {
name: 'Fruits',
y: 2,
drilldown: true
}, {
name: 'Cars',
y: 4,
drilldown: true
}]
}],
drilldown: {
series: []
}
});
});

Resources