Google Charts is not displaying last row in the data array? - google-visualization

I have data array with 45 rows but only 44 rows are visible on the screen after I am hide columns between 8 and 14 using "setColumns" and then re-enable any of the columns between 8 and 14:
The 45th row has following data:
0:{wf: "10 May 2018, 14:44:36"}
1:{wf: ""}
2:{wf: ""}
3:{wf: ""}
4:{wf: ""}
5:{wf: ""}
6:{wf: ""}
7:{wf: ""}
8:{wf: "69.9"}
9:{wf: "73.2"}
10:{wf: "65.2"}
11:{wf: "73.2"}
12:{wf: "82.8"}
13:{wf: "0"}
14:{wf: "28.6"}
To perform column toggle I use following commands:
performColumnToggle: function (col)
{
// use original unfiltered data
_view = new google.visualization.DataView(_original_data);
// get a key due to splice not keeping keys one delete
var key = $.inArray(col, _toggledColumns);
if (_columns[col]['status'] === 1) {
_columns[col]['status'] = 0;
// delete by key because splice doesn't keep keys on delete
_toggledColumns.splice(key, 1);
} else {
_columns[col]['status'] = 1;
// insert new items with a key and push other columns
_toggledColumns.splice(col, 0, col);
}
// set columns to display
_view.setColumns(_toggledColumns);
console.log(_chart.getDataTable().getNumberOfRows()); // keeps returns 44 rather then 45.
_dashboard.draw(_view);
}
I set _original_data when I first initiate the chart and when I have to update the data in the chart. When I perform console.log(_original_data) I see all 45 rows.
init_chart: function ()
{
_data = new google.visualization.DataTable(_tableData);
_original_data = _data;
// some other code ....
}
updateData: function ()
{
_tableData = data;
_data = new google.visualization.DataTable(_tableData);
_original_data = _data;
// some other code ....
}
Below is an example code
https://jsfiddle.net/Spiker/g0k714h7/
To trigger error take following steps:
Scroll down the page you will notice that we have 45 rows with last row having time date "10 May 2018, 14:44:36"
Scroll back up and click on "Select/Diselect All". You can scroll down the table to see that now we have 44 rows
Either Click on "Select/Diselect All" or "record8". Scroll down the page to see that we still have 44 rows in the table

DataView Class
.hideColumns() Method
DataView Class is a special version of the DataTable Class. Its main purpose is to present a part(s) of the DataTable(s). OP code needs to toggle columns with the method that compliments .setColumns() which is .hideColumns().
In Demo 1 below there is a ternary that toggles the columns from [0,...14] to [8...14] columns by using both set/hideColumns() methods. The table has 90 rows and it persists at 90 between toggling of columns.
Example
var dView = *status === 'on' ?
view.setColumns([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]) :
view.hideColumns([0, 1, 2, 3, 4, 5, 6, 7]);
⭐ Demo 2 (located at the bottom of this post,) isn't a solution but it could help the OP code should you decide on not using Demo 1.
Demo 1
.as-console-wrapper.as-console-wrapper {
max-height: 15px;
color: red
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1, user-scalable=no">
<style>
#import url('https://fonts.googleapis.com/css?family=Raleway');
html {
height: 100%;
width: 100%;
font: 400 16px/1.1 Raleway;
}
body {
position: relative;
overflow: auto;
}
main {
overflow: auto;
}
.btn {
width: 11ch;
font: inherit;
}
.btn.on::after {
content: '\a0ON';
}
.btn.off::after {
content: '\a0OFF';
}
.heavy {
font-weight: 900;
}
.darkBkg {
background: rgba(0, 0, 0, 0.4);
color: #fff;
}
.active {
background: rgba(200, 150, 20, 0.3);
color: #000;
}
.tomato {
color: tomato
}
.font {
font-family: Raleway;
}
.right {
text-align: right;
}
.big {
font-size: 20px
}
</style>
</head>
<body>
<main>
<button class='btn off' type='button'>VIEW</button>
<figure id='table'></figure>
</main>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {
'packages': ['table']
});
google.charts.setOnLoadCallback(drawTable);
var cssClassNames = {
'headerRow': 'heavy font',
'tableRow': '',
'oddTableRow': 'darkBkg',
'selectedTableRow': 'active',
'hoverTableRow': 'active',
'headerCell': 'font',
'tableCell': 'right font',
'rowNumberCell': 'big heavy tomato font'
};
var options = {
backgroundColor: {
fill: 'transparent'
},
allowHtml: true,
showRowNumber: true,
width: '100%',
height: '100%',
'cssClassNames': cssClassNames
};
$('.btn').on('click', function(e) {
$(this).toggleClass('on off');
var status = $(this).hasClass('on') ? 'on' : 'off';
console.log(status);
drawTable(status);
});
function drawTable(status) {
var data = new google.visualization.DataTable();
data.addColumn('date', 'START');
data.addColumn('date', 'END');
data.addColumn('string', 'DESC');
data.addColumn('number', '0');
data.addColumn('number', '1');
data.addColumn('number', '2');
data.addColumn('number', '3');
data.addColumn('number', '4');
data.addColumn('number', 'EXT 0');
data.addColumn('number', 'EXT 1');
data.addColumn('number', 'EXT 2');
data.addColumn('number', 'EXT 3');
data.addColumn('number', 'COL 0');
data.addColumn('number', 'COL 1');
data.addColumn('number', 'COL 2');
var time = new google.visualization.DateFormat({
formatType: 'short'
});
data.addRows([
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0],
[new Date(2017, 4, 12), new Date(2019, 4, 12), 'Current', 01, 0.335, 487, 5, 63.20, 52, 23, 105.3, 60, 0.001, 111, -1],
[new Date(1996, 0, 18), new Date(1996, 6, 4), 'Archive', 01, 1.30, 56, 3, 7.08, 88, 23, 98.5, 28, 0.010, 212, 1],
[new Date(2024, 1, 29), new Date(2028, 1, 29), 'Leap', 01, 0.00, 596, 4, 162.2, 971, 23, 92.3, 4, 0.100, 121, 0]
]);
var table = new google.visualization.Table(document.getElementById('table'));
var view = new google.visualization.DataView(data);
var dView = status === 'on' ? view.setColumns([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]) : view.hideColumns([0, 1, 2, 3, 4, 5, 6, 7]);
table.draw(view, options);
}
</script>
<!--<script src='gvis-api.js'></script>-->
</body>
</html>
⭐Although I strongly suggest that you use the hideColumns() from Google Visualization API, I'd like to address this:
"// delete by key because splice doesn't keep keys on delete "
splice() returns whatever it deletes. So you call on splice() and store its return in a var, const, array, object, etc.
Demo 2
var columnArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];
var removedArray = columnArray.splice(0, 8);
console.log('Removed Column Array: ' + removedArray);
console.log('Remaining Column Array: ' + columnArray);
var restoredArray = removedArray.concat(columnArray);
console.log('Restored Column Array: ' + restoredArray);

the problem appears to occur in function --> switchOffAllColumns
there must be some sort of bug with the view
setting the view columns on the ChartWrapper, rather than the DataView,
corrects the issue
in switchOffAllColumns, change the following line...
_view.setColumns(_toggledColumns);
to...
_chart.setView({
columns: _toggledColumns
});
see following working fiddle...
https://jsfiddle.net/WhiteHat/o0j70y9r/
note: jsapi should no longer be used, changed above fiddle to use loader.js load statement...

Related

Pyomo:How to define parameters related to variables

I'm new to Pyomo. I would like to know how to define parameters related to variables? In the following code, i want to express the parameter M.hd[i] is associated with variable M.p_dynamic[i] and other params.Because the M.hd[i] affects the following parameters. Is there any way to solve it? Thanks in advance.
from pyomo.environ import *
# Create a new model
M = ConcreteModel()
M.N = RangeSet(1,96)
M.p_real = Param(M.N,within=NonNegativeReals,initialize=2.0)
M.p_max = Param(M.N,within=NonNegativeReals,initialize=5.0)
M.p_dynamic = Var(M.N,within=NonNegativeReals)
d_base=[5, 8, 10, 9, 9, 5, 9, 4, 5, 4, 8, 3, 5, 2, 3, 3, 3, 1, 0, 19, 4, 0, 0, 0, 0, 1,
3, 5, 8, 14, 26, 28, 70, 84, 130, 145, 144, 71, 77, 76, 84, 60, 100, 55, 46, 42,
52, 50, 53, 44, 77, 55, 66, 39, 63, 67, 71, 41, 62, 62, 67, 47, 64, 62, 69, 50,
75, 47, 43, 26, 38, 36, 42, 36, 52, 24, 24, 22, 23, 14, 16, 7, 11, 5, 7, 7, 4,
4, 4, 0, 1, 0, 1, 0, 1, 0]
M.hd = Param(M.N, within=Integers)
def hd_rule(M,i):
return M.hd[i] == floor(d_base[i-1] * (M.p_max[i] - M.p_dynamic[i]) / (M.p_max[i] - M.p_real[i] ) )
M.hd_rule = Constraint(M.N,rule=hd_rule)
You can replace
M.hd = Param(M.N, within=Integers)
with
M.hd = Var(M.N, within=Integers)
This makes it possible to create the constraints using the hd_rule.

ofstream writing everything to my file twice

So I have a short program to generate a cost matrix to a file but when I run it everything is showing up in the file twice.
Here's what I have:
#include <cstdlib>
#include <fstream>
using namespace std;
int main(){
int V = rand() % 15 + 5;
int graph[V][V];
for( int i = 0; i < V; i++ ){
for( int j = 0; j < V; j++ ){
graph[i][j] = rand() % 50;
}
}
for( int i = 0; i < V; i++ ){
graph[i][i] = 0;
}
ofstream fout;
fout.open( "graphTest.txt", ios::app );
fout << V << endl;
for( int i = 0; i < V; i++ ){
for( int j = 0; j < V; j++ ){
fout << graph[i][j] << ", ";
}
fout << endl;
}
fout.close();
return 0;
}
And here's what the file shows:
18
0, 27, 15, 43, 35, 36, 42, 49, 21, 12, 27, 40, 9, 13, 26, 40, 26, 22,
36, 0, 18, 17, 29, 32, 30, 12, 23, 17, 35, 29, 2, 22, 8, 19, 17, 43,
6, 11, 0, 29, 23, 21, 19, 34, 37, 48, 24, 15, 20, 13, 26, 41, 30, 6,
23, 12, 20, 0, 31, 5, 25, 34, 27, 36, 5, 46, 29, 13, 7, 24, 45, 32,
45, 14, 17, 34, 0, 43, 0, 37, 8, 26, 28, 38, 34, 3, 1, 4, 49, 32,
10, 26, 18, 39, 12, 0, 36, 44, 39, 45, 20, 34, 28, 17, 1, 47, 2, 17,
42, 2, 6, 1, 30, 36, 0, 15, 39, 44, 19, 40, 29, 31, 17, 47, 21, 31,
25, 9, 27, 17, 6, 47, 3, 0, 15, 6, 33, 19, 24, 28, 21, 32, 29, 3,
19, 20, 18, 8, 15, 40, 49, 46, 0, 18, 45, 46, 1, 21, 5, 29, 38, 14,
28, 41, 0, 43, 0, 34, 14, 24, 14, 0, 6, 43, 41, 27, 15, 9, 36, 32,
1, 37, 28, 25, 7, 24, 21, 8, 45, 29, 0, 35, 43, 18, 28, 43, 11, 28,
29, 26, 4, 43, 13, 13, 38, 6, 40, 4, 18, 0, 38, 19, 17, 17, 46, 24,
43, 20, 33, 40, 49, 22, 25, 44, 40, 5, 39, 4, 0, 19, 32, 42, 14, 47,
7, 5, 4, 48, 11, 22, 28, 49, 43, 46, 18, 40, 22, 0, 10, 5, 1, 11,
30, 28, 5, 20, 36, 44, 26, 22, 15, 8, 16, 32, 8, 24, 0, 12, 24, 0,
36, 2, 49, 29, 0, 18, 21, 23, 31, 31, 30, 33, 44, 10, 13, 0, 31, 49,
46, 9, 23, 13, 18, 40, 45, 26, 16, 34, 40, 40, 34, 26, 42, 36, 0, 45,
6, 29, 18, 37, 12, 48, 22, 9, 9, 36, 10, 42, 37, 6, 1, 13, 22, 0,
18
0, 27, 15, 43, 35, 36, 42, 49, 21, 12, 27, 40, 9, 13, 26, 40, 26, 22,
36, 0, 18, 17, 29, 32, 30, 12, 23, 17, 35, 29, 2, 22, 8, 19, 17, 43,
6, 11, 0, 29, 23, 21, 19, 34, 37, 48, 24, 15, 20, 13, 26, 41, 30, 6,
23, 12, 20, 0, 31, 5, 25, 34, 27, 36, 5, 46, 29, 13, 7, 24, 45, 32,
45, 14, 17, 34, 0, 43, 0, 37, 8, 26, 28, 38, 34, 3, 1, 4, 49, 32,
10, 26, 18, 39, 12, 0, 36, 44, 39, 45, 20, 34, 28, 17, 1, 47, 2, 17,
42, 2, 6, 1, 30, 36, 0, 15, 39, 44, 19, 40, 29, 31, 17, 47, 21, 31,
25, 9, 27, 17, 6, 47, 3, 0, 15, 6, 33, 19, 24, 28, 21, 32, 29, 3,
19, 20, 18, 8, 15, 40, 49, 46, 0, 18, 45, 46, 1, 21, 5, 29, 38, 14,
28, 41, 0, 43, 0, 34, 14, 24, 14, 0, 6, 43, 41, 27, 15, 9, 36, 32,
1, 37, 28, 25, 7, 24, 21, 8, 45, 29, 0, 35, 43, 18, 28, 43, 11, 28,
29, 26, 4, 43, 13, 13, 38, 6, 40, 4, 18, 0, 38, 19, 17, 17, 46, 24,
43, 20, 33, 40, 49, 22, 25, 44, 40, 5, 39, 4, 0, 19, 32, 42, 14, 47,
7, 5, 4, 48, 11, 22, 28, 49, 43, 46, 18, 40, 22, 0, 10, 5, 1, 11,
30, 28, 5, 20, 36, 44, 26, 22, 15, 8, 16, 32, 8, 24, 0, 12, 24, 0,
36, 2, 49, 29, 0, 18, 21, 23, 31, 31, 30, 33, 44, 10, 13, 0, 31, 49,
46, 9, 23, 13, 18, 40, 45, 26, 16, 34, 40, 40, 34, 26, 42, 36, 0, 45,
6, 29, 18, 37, 12, 48, 22, 9, 9, 36, 10, 42, 37, 6, 1, 13, 22, 0,
I'm not sure why it's looping through twice
You opened your file with ios::app flags, such it appends the data to an existing file from a former run:
fout.open( "graphTest.txt", ios::app );
// ^^^^^^^^
Open your file using the ios::trunc flag to overwrite the data from previous runs:
fout.open( "graphTest.txt", ios::trunc );
// ^^^^^^^^^^
As a side note:
The reason why you are seeing the same sequence in the file as the product of subsequent runs your program is because you missed to initialize the random generator. You have to call e.g.
seed(time(NULL));
once in the beginning of main().

Google Charts Combochart with Candlesticks

I am trying to create a combochart with candlesticks and a trend line.
<html>
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
[Date(2016, 2, 14, 8, 0), 20, 28, 38, 45],
[Date(2016, 2, 14, 8, 1), 31, 38, 55, 66],
[Date(2016, 2, 14, 8, 2), 50, 55, 77, 80],
[Date(2016, 2, 14, 8, 3), 77, 77, 66, 66]
], true);
var options = {
legend: 'none',
bar: { groupWidth: '90%' },
candlestick: {
fallingColor: { strokeWidth: 0, fill: '#a52714' }, // red
risingColor: { strokeWidth: 0, fill: '#0f9d58' } // green
},
seriesType: 'candlesticks',
series: {5: {type: 'line'}}
};
var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="chart_div"></div>
</body>
</html>
This creates a candlestick chart, but when I add data for a trend line, I get error message "Last domain does not have enough data columns (missing 3)."
[Date(2016, 2, 14, 8, 0), 20, 28, 38, 45, 25],
[Date(2016, 2, 14, 8, 1), 31, 38, 55, 66, 30],
[Date(2016, 2, 14, 8, 2), 50, 55, 77, 80, 35],
[Date(2016, 2, 14, 8, 3), 77, 77, 66, 66, 40]
I figured it out.
series: {1: {type: 'line'}}

How to use vAxis.direction to reverse the order of google graph

I guess I can reverse the order of vertical axis of a graph using vAxis.direction by setting it to -1. But I am just learning and confused with the way to write it.
Here is the current graph:
I want to reverse order the vertical axis with 0 at top and 80 at origin.
Here is the existing code:
google.load('visualization', '1', {packages: ['corechart', 'line']});
google.setOnLoadCallback(drawBackgroundColor);
function drawBackgroundColor() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'X');
data.addColumn('number', 'Dogs');
data.addRows([
[0, 0], [1, 10], [2, 23], [3, 17], [4, 18], [5, 9],
[6, 11], [7, 27], [8, 33], [9, 40], [10, 32], [11, 35],
[12, 30], [13, 40], [14, 42], [15, 47], [16, 44], [17, 48],
[18, 52], [19, 54], [20, 42], [21, 55], [22, 56], [23, 57],
[24, 60], [25, 50], [26, 52], [27, 51], [28, 49], [29, 53],
[30, 55], [31, 60], [32, 61], [33, 59], [34, 62], [35, 65],
[36, 62], [37, 58], [38, 55], [39, 61], [40, 64], [41, 65],
[42, 63], [43, 66], [44, 67], [45, 69], [46, 69], [47, 70],
[48, 72], [49, 68], [50, 66], [51, 65], [52, 67], [53, 70],
[54, 71], [55, 72], [56, 73], [57, 75], [58, 70], [59, 68],
[60, 64], [61, 60], [62, 65], [63, 67], [64, 68], [65, 69],
[66, 70], [67, 72], [68, 75], [69, 80]
]);
var options = {
hAxis: {
title: 'Time'
},
vAxis: {
title: 'Popularity'
},
backgroundColor: '#f1f8e9'
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
Plese suggest how can I set vAxis.direction to -1. I tried a few code but of no help.
try adding a line to your vAxis properties:
vAxis: {
title: 'Popularity',
direction: '-1'
},

NvTriStripe: Generates stripes with duplicate indices

Using NvTriStrip library from Nvidia,
http://www.nvidia.in/object/nvtristrip_library.html
I was trying to use NVTriStriper for creating triangle stripes from triangle indices. But, seems it does not work perfectly.
The following is the input indices,
PrimitiveGroup *pPrimitiveGroup = NULL;
uint16 NbGroups;
uint16 ind[] = {1, 2, 3, 1, 3, 4, 0, 1, 4, 5, 0, 4, 61, 0, 5, 60, 61, 5, 59, 60, 5, 59, 5, 6, 6, 58, 59, 57, 58, 6, 7, 57, 6, 56, 57, 7, 8, 56, 7, 21, 56, 8, 21, 8, 9, 9, 20, 21, 19, 20, 9, 18, 19, 9, 18, 9, 10, 18, 10, 17, 11, 17, 10, 16, 17, 11, 16, 11, 15, 15, 11, 12, 15, 12, 14, 14, 12, 13, 21, 22, 56, 22, 55, 56, 22, 54, 55, 23, 54, 22, 53, 54, 23, 53, 23, 24, 52, 53, 24, 36, 52, 24, 36, 24, 25, 35, 36, 25, 25, 26, 35, 27, 35, 26, 27, 34, 35, 34, 27, 33, 33, 27, 28, 32, 33, 28, 31, 32, 28, 28, 30, 31, 30, 28, 29, 51, 52, 36, 37, 51, 36, 37, 50, 51, 37, 49, 50, 37, 48, 49, 48, 37, 38, 48, 38, 47, 39, 47, 38, 47, 39, 46, 46, 39, 45, 45, 39, 40, 44, 45, 40, 44, 40, 41, 43, 44, 41, 43, 41, 42};
uint16 count=180;
GenerateStrips(ind, count, &pPrimitiveGroup, &NbGroups, true);
The output generated is,
NbGroups = 1
(*pPrimitiveGroup).numIndices = 101
And stripe indices as follows,
outputIndices={10, 10, 17, 11, 16, 11, 15, 12, 14, 13, 13, 54, 54, 54, 22, 55, 22, 56, 21, 8, 21, 9, 20, 9, 19, 9, 18, 10, 17, 17, 2, 2, 2, 3, 1, 4, 0, 5, 61, 5, 60, 5, 59, 6, 58, 6, 57, 7, 56, 8, 8, 24, 24, 24, 36, 25, 35, 26, 35, 27, 34, 27, 33, 28, 32, 28, 31, 28, 30, 29, 29, 42, 42, 43, 41, 44, 40, 45, 39, 46, 39, 47, 38, 48, 37, 37, 48, 48, 37, 49, 37, 50, 37, 51, 36, 52, 24, 53, 23, 54, 22}
Now, with this out as you can see, there are triangles with indices such as (56, 8, 8), this results in rendering lines and not triangles.
Anyone aware of such problems in NVTriStrip?
there are triangles with indices such as (56, 8, 8), this results in rendering lines and not triangles.
No, they result in rendering nothing. The triangle rasterizer renders the area of a triangle; a triangle composed of 2 points has no area and therefore is not rendered.
And yes, this is expected. That's how NVTriStrip works. This is a common technique called "stitching": multiple triangle strips are tied together with degenerate triangles (triangles with no area). This is what allows you to draw the entire model with a single glDrawElements call (or DrawIndexedPrimitive, if you use that).