app/assets/javascripts/pghero/chartkick.js in pghero-1.4.2 vs app/assets/javascripts/pghero/chartkick.js in pghero-1.5.0

- old
+ new

@@ -1,10 +1,10 @@ /* * Chartkick.js * Create beautiful JavaScript charts with minimal code * https://github.com/ankane/chartkick.js - * v2.0.1 + * v2.1.0 * MIT License */ /*jslint browser: true, indent: 2, plusplus: true, vars: true */ @@ -60,14 +60,14 @@ DECIMAL_SEPARATOR = String(1.5).charAt(1); function parseISO8601(input) { var day, hour, matches, milliseconds, minutes, month, offset, result, seconds, type, year; type = Object.prototype.toString.call(input); - if (type === '[object Date]') { + if (type === "[object Date]") { return input; } - if (type !== '[object String]') { + if (type !== "[object String]") { return; } matches = input.match(ISO8601_PATTERN); if (matches) { year = parseInt(matches[1], 10); @@ -81,11 +81,11 @@ if (matches[13] && matches[14]) { offset = matches[15] * 60; if (matches[17]) { offset += parseInt(matches[17], 10); } - offset *= matches[14] === '-' ? -1 : 1; + offset *= matches[14] === "-" ? -1 : 1; result -= offset * 60 * 1000; } return new Date(result); } } @@ -162,22 +162,41 @@ setText(element, "Error Loading Chart: " + message); element.style.color = "#ff0000"; } function getJSON(element, url, success) { - var $ = window.jQuery || window.Zepto || window.$; - $.ajax({ - dataType: "json", - url: url, - success: success, - error: function (jqXHR, textStatus, errorThrown) { - var message = (typeof errorThrown === "string") ? errorThrown : errorThrown.message; - chartError(element, message); - } + ajaxCall(url, success, function (jqXHR, textStatus, errorThrown) { + var message = (typeof errorThrown === "string") ? errorThrown : errorThrown.message; + chartError(element, message); }); } + function ajaxCall(url, success, error) { + var $ = window.jQuery || window.Zepto || window.$; + + if ($) { + $.ajax({ + dataType: "json", + url: url, + success: success, + error: error + }); + } else { + var xhr = new XMLHttpRequest(); + xhr.open("GET", url, true); + xhr.setRequestHeader("Content-Type", "application/json"); + xhr.onload = function () { + if (xhr.status === 200) { + success(JSON.parse(xhr.responseText), xhr.statusText, xhr); + } else { + error(xhr, "error", xhr.statusText); + } + }; + xhr.send(); + } + } + function errorCatcher(chart, callback) { try { callback(chart); } catch (err) { chartError(chart.element, err.message); @@ -342,11 +361,13 @@ } }; } var options = jsOptions(chart.data, chart.options, chartOptions), data, i, j; options.xAxis.type = chart.options.discrete ? "category" : "datetime"; - options.chart.type = chartType; + if (!options.chart.type) { + options.chart.type = chartType; + } options.chart.renderTo = chart.element.id; var series = chart.data; for (i = 0; i < series.length; i++) { data = series[i].data; @@ -362,11 +383,11 @@ }; this.renderScatterChart = function (chart) { var chartOptions = {}; var options = jsOptions(chart.data, chart.options, chartOptions); - options.chart.type = 'scatter'; + options.chart.type = "scatter"; options.chart.renderTo = chart.element.id; options.series = chart.data; new Highcharts.Chart(options); }; @@ -439,11 +460,11 @@ self.renderLineChart(chart, "areaspline"); }; }; adapters.push(HighchartsAdapter); } - if (!GoogleChartsAdapter && window.google && window.google.setOnLoadCallback) { + if (!GoogleChartsAdapter && window.google && (window.google.setOnLoadCallback || window.google.charts)) { GoogleChartsAdapter = new function () { var google = window.google; this.name = "google"; @@ -482,11 +503,16 @@ callback: runCallbacks }; if (config.language) { loadOptions.language = config.language; } - google.load("visualization", "1", loadOptions); + + if (window.google.setOnLoadCallback) { + google.load("visualization", "1", loadOptions); + } else { + google.charts.load("current", loadOptions); + } } }; // Set chart options var defaultOptions = { @@ -1004,20 +1030,20 @@ options.scales.xAxes[0].time.unit = "month"; step = 30; } else if (day || timeDiff > 10) { options.scales.xAxes[0].time.unit = "day"; step = 1; - } else if (hour) { + } else if (hour || timeDiff > 0.5) { + options.scales.xAxes[0].time.displayFormats = {hour: "MMM D, h a"}; options.scales.xAxes[0].time.unit = "hour"; step = 1 / 24.0; } else if (minute) { options.scales.xAxes[0].time.displayFormats = {minute: "h:mm a"}; options.scales.xAxes[0].time.unit = "minute"; step = 1 / 24.0 / 60.0; } - if (step && timeDiff > 0) { var unitStepSize = Math.ceil(timeDiff / step / (chart.element.offsetWidth / 100.0)); if (week && step === 1) { unitStepSize = Math.ceil(unitStepSize / 7.0) * 7; } @@ -1383,10 +1409,17 @@ setElement(this, element, dataSource, opts, processScatterData); }, Timeline: function (element, dataSource, opts) { setElement(this, element, dataSource, opts, processTimelineData); }, - charts: {} + charts: {}, + configure: function (options) { + for (var key in options) { + if (options.hasOwnProperty(key)) { + config[key] = options[key]; + } + } + } }; if (typeof module === "object" && typeof module.exports === "object") { module.exports = Chartkick; } else {