Skip to content

Commit

Permalink
Added a global lookup array, Highcharts.charts, holding all available…
Browse files Browse the repository at this point in the history
… chart instances in the page. This prevents issues with writing multiple charts to the same div.
  • Loading branch information
TorsteinHonsi committed Oct 25, 2012
1 parent af42f86 commit c0e2b87
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 8 deletions.
22 changes: 20 additions & 2 deletions js/highcharts.src.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var UNDEFINED,
pathAnim,
timeUnits,
noop = function () {},
charts = [],

// some constants for frequently used strings
DIV = 'div',
Expand Down Expand Up @@ -10352,6 +10353,8 @@ Chart.prototype = {
chartWidth,
chartHeight,
renderTo,
indexAttrName = 'data-highcharts-chart',
oldChartIndex,
containerId;

chart.renderTo = renderTo = optionsChart.renderTo;
Expand All @@ -10365,6 +10368,15 @@ Chart.prototype = {
if (!renderTo) {
error(13, true);
}

// If the container already holds a chart, destroy it
oldChartIndex = pInt(attr(renderTo, indexAttrName));
if (!isNaN(oldChartIndex) && charts[oldChartIndex]) {
charts[oldChartIndex].destroy();
}

// Make a reference to the chart from the div
attr(renderTo, indexAttrName, chart.index);

// remove previous chart
renderTo.innerHTML = '';
Expand Down Expand Up @@ -10994,9 +11006,12 @@ Chart.prototype = {
container = chart.container,
i,
parentNode = container && container.parentNode;

// fire the chart.destoy event
fireEvent(chart, 'destroy');

// Delete the chart from charts lookup array
charts[chart.index] = UNDEFINED;

// remove events
removeEvent(chart);
Expand Down Expand Up @@ -11133,7 +11148,9 @@ Chart.prototype = {
optionsChart = chart.options.chart,
eventType;

// Run chart
// Add the chart to the global lookup
chart.index = charts.length;
charts.push(chart);

// Set up auto resize
if (optionsChart.reflow !== false) {
Expand Down Expand Up @@ -15187,6 +15204,7 @@ extend(Highcharts, {
// Various
arrayMin: arrayMin,
arrayMax: arrayMax,
charts: charts, // docs
dateFormat: dateFormat,
format: format,
pathAnim: pathAnim,
Expand Down
22 changes: 20 additions & 2 deletions js/highstock.src.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var UNDEFINED,
pathAnim,
timeUnits,
noop = function () {},
charts = [],

// some constants for frequently used strings
DIV = 'div',
Expand Down Expand Up @@ -10352,6 +10353,8 @@ Chart.prototype = {
chartWidth,
chartHeight,
renderTo,
indexAttrName = 'data-highcharts-chart',
oldChartIndex,
containerId;

chart.renderTo = renderTo = optionsChart.renderTo;
Expand All @@ -10365,6 +10368,15 @@ Chart.prototype = {
if (!renderTo) {
error(13, true);
}

// If the container already holds a chart, destroy it
oldChartIndex = pInt(attr(renderTo, indexAttrName));
if (!isNaN(oldChartIndex) && charts[oldChartIndex]) {
charts[oldChartIndex].destroy();
}

// Make a reference to the chart from the div
attr(renderTo, indexAttrName, chart.index);

// remove previous chart
renderTo.innerHTML = '';
Expand Down Expand Up @@ -10994,9 +11006,12 @@ Chart.prototype = {
container = chart.container,
i,
parentNode = container && container.parentNode;

// fire the chart.destoy event
fireEvent(chart, 'destroy');

// Delete the chart from charts lookup array
charts[chart.index] = UNDEFINED;

// remove events
removeEvent(chart);
Expand Down Expand Up @@ -11133,7 +11148,9 @@ Chart.prototype = {
optionsChart = chart.options.chart,
eventType;

// Run chart
// Add the chart to the global lookup
chart.index = charts.length;
charts.push(chart);

// Set up auto resize
if (optionsChart.reflow !== false) {
Expand Down Expand Up @@ -18539,6 +18556,7 @@ extend(Highcharts, {
// Various
arrayMin: arrayMin,
arrayMax: arrayMax,
charts: charts, // docs
dateFormat: dateFormat,
format: format,
pathAnim: pathAnim,
Expand Down
20 changes: 18 additions & 2 deletions js/parts/Chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,8 @@ Chart.prototype = {
chartWidth,
chartHeight,
renderTo,
indexAttrName = 'data-highcharts-chart',
oldChartIndex,
containerId;

chart.renderTo = renderTo = optionsChart.renderTo;
Expand All @@ -673,6 +675,15 @@ Chart.prototype = {
if (!renderTo) {
error(13, true);
}

// If the container already holds a chart, destroy it
oldChartIndex = pInt(attr(renderTo, indexAttrName));
if (!isNaN(oldChartIndex) && charts[oldChartIndex]) {
charts[oldChartIndex].destroy();
}

// Make a reference to the chart from the div
attr(renderTo, indexAttrName, chart.index);

// remove previous chart
renderTo.innerHTML = '';
Expand Down Expand Up @@ -1302,9 +1313,12 @@ Chart.prototype = {
container = chart.container,
i,
parentNode = container && container.parentNode;

// fire the chart.destoy event
fireEvent(chart, 'destroy');

// Delete the chart from charts lookup array
charts[chart.index] = UNDEFINED;

// remove events
removeEvent(chart);
Expand Down Expand Up @@ -1441,7 +1455,9 @@ Chart.prototype = {
optionsChart = chart.options.chart,
eventType;

// Run chart
// Add the chart to the global lookup
chart.index = charts.length;
charts.push(chart);

// Set up auto resize
if (optionsChart.reflow !== false) {
Expand Down
1 change: 1 addition & 0 deletions js/parts/Facade.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extend(Highcharts, {
// Various
arrayMin: arrayMin,
arrayMax: arrayMax,
charts: charts, // docs
dateFormat: dateFormat,
format: format,
pathAnim: pathAnim,
Expand Down
1 change: 1 addition & 0 deletions js/parts/Globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var UNDEFINED,
pathAnim,
timeUnits,
noop = function () {},
charts = [],

// some constants for frequently used strings
DIV = 'div',
Expand Down
16 changes: 14 additions & 2 deletions samples/data/updatejson.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ function makeJSON ($q, $startdate, $enddate, $props) {

foreach ($csv as $line) {
list ($date, $open, $high, $low, $close, $volume) = explode(',', $line);

$date = strtotime("$date UTC");
$thisMonth = date('M Y', $date);

Expand All @@ -40,6 +39,18 @@ function makeJSON ($q, $startdate, $enddate, $props) {
if ($open < $low || $open == '') {
$open = $low;
}
if ($open < 0.01) {
$open = 'null';
}
if ($high < 0.01) {
$high = 'null';
}
if ($low < 0.01) {
$low = 'null';
}
if ($close < 0.01) {
$close = 'null';
}

// push it
if ($props == 'c') {
Expand All @@ -58,13 +69,14 @@ function makeJSON ($q, $startdate, $enddate, $props) {
}

$s = "/* $q historical OHLC data from the Google Finance API */\n[\n". join(",\n", $arr) . "\n]";
//$s = "/* AAPL historical OHLC data from the Google Finance API */\n[\n". join(";\n", $arr) . "\n]";
// $s = "/* AAPL historical OHLC data from the Google Finance API */\n[\n". join(";\n", $arr) . "\n]";



// write the files
$q = strtolower($q);
file_put_contents("$q-$props.json", $s);
//echo $s;
}
//makeJSON('AAPL', $startdate, $enddate, 'ohlc');

Expand Down

0 comments on commit c0e2b87

Please sign in to comment.