Use result of get function within a javascript - charts

I'm trying to impliment a google chart like the no dependancies one shown here
To get this to work with my data I'm importing the data for the rows like so..
$.get('http://104.12.156.29:8011/java/servlet/UTRICKC5.I00120s', function (data2) {
console.log(data2);});
This pulls what I need... but how can I replace the data.addrows block with what I'm pulling in?
For reference, what I'm getting shown in the console is this;
['Task 1', 'Task 1','Team 1',new Date(2014 , 02, 01), new Date(2014 , 02, 10), null, 50, null],
['Task 2', 'Task 2','Team 2',new Date(2014 , 03, 01), new Date(2014 , 04, 01), null, 1, null]
So I pretty just need to find a way of getting this to "render" as part of my javascript.
<script type="text/javascript">
google.charts.load('current', {'packages':['gantt']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Task ID');
data.addColumn('string', 'Task Name');
data.addColumn('string', 'Resource');
data.addColumn('date', 'Start Date');
data.addColumn('date', 'End Date');
data.addColumn('number', 'Duration');
data.addColumn('number', 'Percent Complete');
data.addColumn('string', 'Dependencies');
$.get('http://http://104.12.156.29:8011/java/servlet/UTRICKC5.I00120s', function (data2) {
console.log(data2);});
data.addRows([
['2014Spring', 'Spring 2014', 'spring',
new Date(2014, 2, 22), new Date(2014, 5, 20), null, 100, null],
['2014Summer', 'Summer 2014', 'summer',
new Date(2014, 5, 21), new Date(2014, 8, 20), null, 100, null],
['2014Autumn', 'Autumn 2014', 'autumn',
new Date(2014, 8, 21), new Date(2014, 11, 20), null, 100, null],
['2014Winter', 'Winter 2014', 'winter',
new Date(2014, 11, 21), new Date(2015, 2, 21), null, 100, null],
['2015Spring', 'Spring 2015', 'spring',
new Date(2015, 2, 22), new Date(2015, 5, 20), null, 50, null],
['2015Summer', 'Summer 2015', 'summer',
new Date(2015, 5, 21), new Date(2015, 8, 20), null, 0, null],
['2015Autumn', 'Autumn 2015', 'autumn',
new Date(2015, 8, 21), new Date(2015, 11, 20), null, 0, null],
['2015Winter', 'Winter 2015', 'winter',
new Date(2015, 11, 21), new Date(2016, 2, 21), null, 0, null],
['Football', 'Football Season', 'sports',
new Date(2014, 8, 4), new Date(2015, 1, 1), null, 100, null],
['Baseball', 'Baseball Season', 'sports',
new Date(2015, 2, 31), new Date(2015, 9, 20), null, 14, null],
['Basketball', 'Basketball Season', 'sports',
new Date(2014, 9, 28), new Date(2015, 5, 20), null, 86, null],
['Hockey', 'Hockey Season', 'sports',
new Date(2014, 9, 8), new Date(2015, 5, 21), null, 89, null]
]);
var options = {
height: 400,
gantt: {
trackHeight: 30
}
};
var chart = new google.visualization.Gantt(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>

first, recommend formatting the data as json, as found here...
{
"cols": [
{"label": "Task ID", "type": "string"},
{"label": "Task Name", "type": "string"},
{"label": "Resource", "type": "string"},
{"label": "Start Date", "type": "date"},
{"label": "End Date", "type": "date"},
{"label": "Duration", "type": "number"},
{"label": "Percent Complete", "type": "number"},
{"label": "Dependencies", "type": "string"}
],
"rows": [
{"c":[{"v": "Task 1"}, {"v": "Task 1"}, {"v": "Team 1"}, {"v": "Date(2014, 2, 1)"}, {"v": "Date(2014, 2, 10)"}, {"v": null}, {"v": 50}, {"v": null}]},
{"c":[{"v": "Task 2"}, {"v": "Task 2"}, {"v": "Team 2"}, {"v": "Date(2014, 3, 1)"}, {"v": "Date(2014, 4, 1)"}, {"v": null}, {"v": 1}, {"v": null}]}
]
}
this will allow you to create the data table directly, without client-side manipulation.
and it is the fastest way to load the data table.
next, both $.get and $.ajax are asynchronous,
which means you will need to wait until the data is received,
before trying to create the data table and draw the chart.
recommend using $.ajax so you can specify the dataType as json.
see following working snippet,
here, I use the fail promise method so the snippet will work,
you can remove on your server and just keep the done method.
google.charts.load('current', {
packages: ['gantt']
}).then(function () {
var chart = new google.visualization.Gantt(document.getElementById('chart_div'));
var options = {
height: 400,
gantt: {
trackHeight: 30
}
};
$.ajax({
url: 'http://104.12.156.29:8011/java/servlet/UTRICKC5.I00120s',
dataType: 'json'
}).done(drawChart).fail(function () {
var chartData = {
"cols": [
{"label": "Task ID", "type": "string"},
{"label": "Task Name", "type": "string"},
{"label": "Resource", "type": "string"},
{"label": "Start Date", "type": "date"},
{"label": "End Date", "type": "date"},
{"label": "Duration", "type": "number"},
{"label": "Percent Complete", "type": "number"},
{"label": "Dependencies", "type": "string"}
],
"rows": [
{"c":[{"v": "Task 1"}, {"v": "Task 1"}, {"v": "Team 1"}, {"v": "Date(2014, 2, 1)"}, {"v": "Date(2014, 2, 10)"}, {"v": null}, {"v": 50}, {"v": null}]},
{"c":[{"v": "Task 2"}, {"v": "Task 2"}, {"v": "Team 2"}, {"v": "Date(2014, 3, 1)"}, {"v": "Date(2014, 4, 1)"}, {"v": null}, {"v": 1}, {"v": null}]}
]
};
drawChart(chartData);
});
function drawChart(chartData) {
var data = new google.visualization.DataTable(chartData);
chart.draw(data, options);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

Related

Compare elements in two lists and make a new List in Dart

I have two lists and I want to make a new list by comparing the two lists and the new list should only contain those elements that contain in each of them.
List1=[[id: 1, label: 'cocoa'],[id: 2, label: 'apple'],[id: 3, label: 'cherry'],[id: 4, label: 'banana'],[id: 5, label: 'melon'],[id: 6, label: 'orange'],[id: 7, label: 'pineapple'],[id: 8], label: 'strawberry']
List2=[2,5,7,8]
expectedList = [[id: 2, label: 'apple'],[id: 5, label: 'melon'],[id: 7, label: 'pineapple'],[id: 8], label: 'strawberry']]
And the real code down below:
"options": [
{
"id": 58,
"label": "cocoa",
"swatch_value": null,
"products": [
118
]
},
{
"id": 59,
"label": "dark chocolate",
"swatch_value": null,
"products": [
120,
127
]
},
{
"id": 60,
"label": "apple",
"swatch_value": null,
"products": [
121,
128
]
},
{
"id": 61,
"label": "milk",
"swatch_value": null,
"products": [
122
]
},
{
"id": 62,
"label": "coconut",
"swatch_value": null,
"products": [
130
]
},
{
"id": 65,
"label": "cherry",
"swatch_value": null,
"products": [
126
]
}
]
So the first list would contain the json above, and the second list contains the numbers below that equals to some of the ID.
List<int> secondList = [58, 59, 60, 61];
Now when I want to generate a new list by comparing the secondList with the ID's of the first list, the third list is empty.
List thirdList = firstList.options.where((element) => secondList.contains(element.id)).toList();
var list1 = [1,2,3,4,5,6,7,8];
var list2 = [2,5,7,8];
var expectedList = list1.toSet().intersection(list2.toSet()).toList();
print(expectedList.toString());
More information here.
Respect to your info:
var dataList = data['options'];
var firstList = List<int>.generate(dataList.length, (i) => dataList[i]['id']);
var secondList = [58, 59, 60, 61];
var thirdList = firstList.toSet().intersection(secondList.toSet()).toList();
var filtered = dataList.where((e) => thirdList.contains(e['id'])).toList();
print(filtered.toString());
where:
const data = {
"options": [
...
]
};
The result is:
[{id: 58, label: cocoa, swatch_value: null, products: [118]}, {id: 59, label: dark chocolate, swatch_value: null, products: [120, 127]}, {id: 60, label: apple, swatch_value: null, products: [121, 128]}, {id: 61, label: milk, swatch_value: null, products: [122]}]

Binding dyanmic data to DropdownBox in SAPUI5 table

I want to bind the dynamic Dropdowns for each row in SAPUI5 table. There should be the row-specific data for each row-Dropdown. Here is the sample json having three rows. But I'm unable to bind the row-specific data for each dropdown. Thanks in Advance!
Here, I have simple table.
// Table goes here
var demoTbl = new sap.ui.table.Table({
visibleRowCount: 10,
width : "100%",
selectionMode: sap.ui.table.SelectionMode.Multi,
});
var systemColumn = new sap.ui.table.Column({
width:"12%",
label: new sap.ui.commons.Label({text: "System", design:sap.ui.commons.LabelDesign.Bold}),
template: new sap.ui.commons.TextField({editable:false}).bindProperty("value", "name"),
sortProperty: "name",
filterProperty: "name",
sorted : false,
filtered : false
});
demoTbl.addColumn(systemColumn);
bind list data for 1st row-Dropdown List here-
var inputListBox = new sap.ui.commons.ListBox();
inputListBox.bindAggregation("items","/listData/dataList/0/dropList",function(oId,oContext){
console.log(oContext);
return new sap.ui.core.ListItem({
key: oContext.getProperty("id"),
text: oContext.getProperty("name")
});
});
var connectorIpColumn = new sap.ui.table.Column({
width:"12%",
label: new sap.ui.commons.Label({text: "Dropdown Data", design:sap.ui.commons.LabelDesign.Bold}),
template: new sap.ui.commons.DropdownBox({
"association:listBox" : inputListBox
})
});
demoTbl.addColumn(connectorIpColumn);
And, here is the Data -
var oData={
"dataList": [{
"id": 111,
"name": "Row1 Data",
"dropList": [
{"id": 1, "name": "Row1 dropDown Item1"},
{"id": 2, "name": "Row1 dropDown Item2"},
{"id": 3, "name": "Row1 dropDown Item3"},
{"id": 4, "name": "Row1 dropDown Item4"}
]
},
{
"id": 222,
"name": "Row2 Data",
"dropList": [
{"id": 5, "name": "Row2 dropDown Item1"},
{"id": 6, "name": "Row2 dropDown Item2"},
{"id": 7, "name": "Row2 dropDown Item3"}
]
},
{
"id": 333,
"name": "Row3 Data",
"dropList": [
{"id": 8, "name": "Row3 dropDown Item1"},
{"id": 9, "name": "Row3 dropDown Item2"},
{"id": 10, "name": "Row3 dropDown Item3"}
]
}
]};
Bind data here-
var mappingModel = new sap.ui.model.json.JSONModel({listData:oData});
sap.ui.getCore().setModel(mappingModel, "mappingModel");
demoTbl.setModel(mappingModel);
demoTbl.bindRows("/listData/dataList");
mappingModel.refresh(true);
var addSystemPage = new sap.m.Page( {
content:[demoTbl]
});
You'll have to provide the aggregation binding path for ListBox template as 'dropList'.
inputListBox.bindAggregation("items","dropList",function(oId,oContext){
return new sap.ui.core.ListItem({
key: oContext.getProperty("id"),
text: oContext.getProperty("name")
});
});

Hide empty DropdownBox in sapui5 table rows

Here is the simple table having three rows, and each row contains a DropdownBox with listItems. But the DropdownBox in the second row is empty. I want to hide the blank DropdownBox. Can we hide the empty DropdownBox from that row, so that it will look just a simple blank cell. Thanks in Advance!
Here, I have simple table.
var demoTbl = new sap.ui.table.Table({
visibleRowCount: 10,
width : "100%",
selectionMode: sap.ui.table.SelectionMode.Multi,
});
var systemColumn = new sap.ui.table.Column({
width:"12%",
label: new sap.ui.commons.Label({text: "Column Data", design:sap.ui.commons.LabelDesign.Bold}),
template: new sap.ui.commons.TextField({editable:false}).bindProperty("value", "name"),
sortProperty: "name",
filterProperty: "name",
sorted : false,
filtered : false
});
demoTbl.addColumn(systemColumn);
var inputListBox = new sap.ui.commons.ListBox();
inputListBox.bindAggregation("items","dropList",function(oId,oContext){
return new sap.ui.core.ListItem({
key: oContext.getProperty("id"),
text: oContext.getProperty("name")
});
});
var connectorIpColumn = new sap.ui.table.Column({
width:"12%",
label: new sap.ui.commons.Label({text: "Dropdown Data", design:sap.ui.commons.LabelDesign.Bold}),
template: new sap.ui.commons.DropdownBox({
"association:listBox" : inputListBox
})
});
demoTbl.addColumn(connectorIpColumn);
And, here is the Data -
var oData={
"dataList": [{
"id": 111,
"name": "Row1 Data",
"dropList": [
{"id": 1, "name": "Row1 dropDown Item1"},
{"id": 2, "name": "Row1 dropDown Item2"},
{"id": 3, "name": "Row1 dropDown Item3"},
{"id": 4, "name": "Row1 dropDown Item4"}
]
},
{
"id": 222,
"name": "Row2 Data",
"dropList": []
},
{
"id": 333,
"name": "Row3 Data",
"dropList": [
{"id": 8, "name": "Row3 dropDown Item1"},
{"id": 9, "name": "Row3 dropDown Item2"},
{"id": 10, "name": "Row3 dropDown Item3"}
]
}
]};
var mappingModel = new sap.ui.model.json.JSONModel({listData:oData});
sap.ui.getCore().setModel(mappingModel, "mappingModel");
demoTbl.setModel(mappingModel);
demoTbl.bindRows("/listData/dataList");
mappingModel.refresh(true);
var addSystemPage = new sap.m.Page("addSystemPageId", {
content:[demoTbl]
});
There are many ways reading the cells of the table and determining the dropdown values and explicitly setting the visibility. I would propose the best way is to
var oData={
"dataList": [{
"id": 111,
"name": "Row1 Data",
"dropVis" : true,
"dropList": [
{"id": 1, "name": "Row1 dropDown Item1"},
{"id": 2, "name": "Row1 dropDown Item2"},
{"id": 3, "name": "Row1 dropDown Item3"},
{"id": 4, "name": "Row1 dropDown Item4"}
]
},
{
"id": 222,
"name": "Row2 Data",
"dropVis" : false,
"dropList": []
},
{
"id": 333,
"name": "Row3 Data",
"dropVis" : true,
"dropList": [
{"id": 8, "name": "Row3 dropDown Item1"},
{"id": 9, "name": "Row3 dropDown Item2"},
{"id": 10, "name": "Row3 dropDown Item3"}
]
}
]};
You can see the json object has been modified to get one attribute dropVis this can manually filled you you based on dropList and finally bind this attribute to the call template
var connectorIpColumn = new sap.ui.table.Column({
width:"12%",
label: new sap.ui.commons.Label({text: "Dropdown Data", design:sap.ui.commons.LabelDesign.Bold}),
template: new sap.ui.commons.DropdownBox({
visible : "{dropVis}",
"association:listBox" : inputListBox
})
});
The visibility is bound directly and it should work.
You can make use of formatter to toggle visibility based on length of dropList Array.
template: new sap.ui.commons.DropdownBox({
visible: {
path: 'dropList',
formatter: function(aList) {
return aList ? !!aList.length : false;
}
}
});

How to make a SERIES line dashed in Google Line Chart?

In Google Line Chart how do you make a series line dashed?
For example the red line (called "Row B") in the screenshot below?
Below is my very simple test code, just open it in a browser and it will work instantly.
Please note that the usual suggestion to add certainty role:
{"p":{"role":"certainty"},"label":"Dashed","type":"boolean"}
doesn't help here, because it would make dashed (parts of) the both lines (the rows "A" and "B").
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['corechart']}]}"></script>
<script type="text/javascript">
var data = {"rows":[
{"c":[{"v":"C"},{"v":-43},{"v":-42}]},
{"c":[{"v":"D"},{"v":-49},{"v":-39}]},
{"c":[{"v":"E"},{"v":-49},{"v":-48}]},
{"c":[{"v":"F"},{"v":-50},{"v":-49}]},
{"c":[{"v":"G"},{"v":-57},{"v":-56}]}],
"cols":[
{"p":{"role":"domain"},"label":"MEASUREMENT","type":"string"},
{"p":{"role":"data"},"label":"Row A","type":"number"},
{"p":{"role":"data"},"label":"Row B","type":"number"}]};
function drawCharts() {
var x = new google.visualization.DataTable(data);
var options = {
title: 'How to make red line dashed?',
width: 800,
height: 600
};
var chart = new google.visualization.LineChart(document.getElementById('test'));
chart.draw(x, options);
}
$(function() {
google.setOnLoadCallback(drawCharts);
});
</script>
</head>
<body>
<div id="test"></div>
</body>
</html>
Here is an example using the certainty role. Is there any reason why this doesn't work for you?
google.load('visualization', '1', {
packages: ['corechart'],
callback: drawVisualization
});
var data = {
"rows": [
{"c": [{"v": "C"}, {"v": -43}, {"v": -42}, {"v": false}]},
{"c": [{"v": "D"}, {"v": -49}, {"v": -39}, {"v": false}]},
{"c": [{"v": "E"}, {"v": -49}, {"v": -48}, {"v": false}]},
{"c": [{"v": "F"}, {"v": -50}, {"v": -49}, {"v": false}]},
{"c": [{"v": "G"}, {"v": -57}, {"v": -56}, {"v": false}]}],
"cols": [
{"p": {"role": "domain"},"label": "MEASUREMENT","type": "string"},
{"p": {"role": "data"},"label": "Row A","type": "number"},
{"p": {"role": "data"},"label": "Row B","type": "number"},
{"p": {"role": "certainty"},"type": "boolean"}]
};
function drawVisualization() {
var x = new google.visualization.DataTable(data);
var options = {
title: 'How to make red line dashed?',
width: 800,
height: 600
};
var chart = new google.visualization.LineChart(document.getElementById('visualization'));
chart.draw(x, options);
}
Here is an example using the certainty role to make both lines dashed.
google.load('visualization', '1', {
packages: ['corechart'],
callback: drawVisualization
});
var data = {
"rows": [
{"c": [{"v": "C"}, {"v": -43}, {"v": false}, {"v": -42}, {"v": false}]},
{"c": [{"v": "D"}, {"v": -49}, {"v": false}, {"v": -39}, {"v": false}]},
{"c": [{"v": "E"}, {"v": -49}, {"v": false}, {"v": -48}, {"v": false}]},
{"c": [{"v": "F"}, {"v": -50}, {"v": false}, {"v": -49}, {"v": false}]},
{"c": [{"v": "G"}, {"v": -57}, {"v": false}, {"v": -56}, {"v": false}]}],
"cols": [
{"p": {"role": "domain"},"label": "MEASUREMENT","type": "string"},
{"p": {"role": "data"},"label": "Row A","type": "number"},
{"p": {"role": "certainty"},"type": "boolean"},
{"p": {"role": "data"},"label": "Row B","type": "number"},
{"p": {"role": "certainty"},"type": "boolean"}]
};
function drawVisualization() {
var x = new google.visualization.DataTable(data);
var options = {
title: 'How to make red line dashed?',
width: 800,
height: 600
};
var chart = new google.visualization.LineChart(document.getElementById('visualization'));
chart.draw(x, options);
}

d3.json pass data to d3.svg.arc()

I'm not strong JS user, but I want make "Nightingale chart" like this: http://windhistory.com/station.html?KHKA
I have that code:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="d3.v2.js"></script>
<style type="text/css">
.arc{
fill: pink;
stroke: red;
}
</style>
</head>
<body>
<div id="chart" class="chart"></div>
<div id="table"></div>
<script type="text/javascript">
var svg = d3.select("#chart").append("svg").attr("width", 900).attr("height", 600);
var pi = Math.PI;
d3.json(
'data.json',
function(data){
var arc = d3.svg.arc()
.innerRadius(50)
.outerRadius(function(d) {
return (50 + d.value);
})
.startAngle(function(d) { return ((d.time - 1) * 30 * pi / 180); })
.endAngle(function(d) { return (d.time * 30 * pi / 180 ); });
var chartContainer = svg.append("g")
.attr('class', 'some_class')
.attr("transform", "translate(450, 300)");
chartContainer.append("path")
.data(data)
.attr("d", arc)
.attr("class", "arc");
}
);
</script>
</body>
</html>
On jsfinddle: http://jsfiddle.net/lmasikl/gZ62Z/
my json:
[
{"label": "January", "value": 150, "time": 1},
{"label": "February", "value": 65, "time": 2},
{"label": "March", "value": 50, "time": 3},
{"label": "April", "value": 75, "time": 4},
{"label": "May", "value": 150, "time": 5},
{"label": "June", "value": 65, "time": 6},
{"label": "July", "value": 50, "time": 7},
{"label": "August", "value": 75, "time": 8},
{"label": "September", "value": 65, "time": 9},
{"label": "October", "value": 50, "time": 10},
{"label": "November", "value": 75, "time": 11},
{"label": "December", "value": 150, "time": 12}
]
But my script draw only one arc. Can anybody help to solve this problem?
You may want to read Thinking With Joins. The D3 pattern for adding data-driven elements is to create a selection with selectAll, then set the data with data, then append the element, to the .enter() selection. So
chartContainer.append("path")
.data(data)
.attr("d", arc)
.attr("class", "arc");
needs to be
chartContainer.selectAll("path")
.data(data)
.enter().append("path")
.attr("d", arc)
.attr("class", "arc");
See updated fiddle: http://jsfiddle.net/gZ62Z/1/