Blame view
static/ueditor/third-party/highcharts/modules/data.src.js
15.1 KB
831eac332 add file |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 |
/** * @license Data plugin for Highcharts * * (c) 2012-2013 Torstein Hønsi * Last revision 2013-06-07 * * License: www.highcharts.com/license */ /* * The Highcharts Data plugin is a utility to ease parsing of input sources like * CSV, HTML tables or grid views into basic configuration options for use * directly in the Highcharts constructor. * * Demo: http://jsfiddle.net/highcharts/SnLFj/ * * --- OPTIONS --- * * - columns : Array<Array<Mixed>> * A two-dimensional array representing the input data on tabular form. This input can * be used when the data is already parsed, for example from a grid view component. * Each cell can be a string or number. If not switchRowsAndColumns is set, the columns * are interpreted as series. See also the rows option. * * - complete : Function(chartOptions) * The callback that is evaluated when the data is finished loading, optionally from an * external source, and parsed. The first argument passed is a finished chart options * object, containing series and an xAxis with categories if applicable. Thise options * can be extended with additional options and passed directly to the chart constructor. * * - csv : String * A comma delimited string to be parsed. Related options are startRow, endRow, startColumn * and endColumn to delimit what part of the table is used. The lineDelimiter and * itemDelimiter options define the CSV delimiter formats. * * - endColumn : Integer * In tabular input data, the first row (indexed by 0) to use. Defaults to the last * column containing data. * * - endRow : Integer * In tabular input data, the last row (indexed by 0) to use. Defaults to the last row * containing data. * * - googleSpreadsheetKey : String * A Google Spreadsheet key. See https://developers.google.com/gdata/samples/spreadsheet_sample * for general information on GS. * * - googleSpreadsheetWorksheet : String * The Google Spreadsheet worksheet. The available id's can be read from * https://spreadsheets.google.com/feeds/worksheets/{key}/public/basic * * - itemDelimiter : String * Item or cell delimiter for parsing CSV. Defaults to ",". * * - lineDelimiter : String * Line delimiter for parsing CSV. Defaults to " ". * * - parsed : Function * A callback function to access the parsed columns, the two-dimentional input data * array directly, before they are interpreted into series data and categories. * * - parseDate : Function * A callback function to parse string representations of dates into JavaScript timestamps. * Return an integer on success. * * - rows : Array<Array<Mixed>> * The same as the columns input option, but defining rows intead of columns. * * - startColumn : Integer * In tabular input data, the first column (indexed by 0) to use. * * - startRow : Integer * In tabular input data, the first row (indexed by 0) to use. * * - table : String|HTMLElement * A HTML table or the id of such to be parsed as input data. Related options ara startRow, * endRow, startColumn and endColumn to delimit what part of the table is used. */ // JSLint options: /*global jQuery */ (function (Highcharts) { // Utilities var each = Highcharts.each; // The Data constructor var Data = function (dataOptions, chartOptions) { this.init(dataOptions, chartOptions); }; // Set the prototype properties Highcharts.extend(Data.prototype, { /** * Initialize the Data object with the given options */ init: function (options, chartOptions) { this.options = options; this.chartOptions = chartOptions; this.columns = options.columns || this.rowsToColumns(options.rows) || []; // No need to parse or interpret anything if (this.columns.length) { this.dataFound(); // Parse and interpret } else { // Parse a CSV string if options.csv is given this.parseCSV(); // Parse a HTML table if options.table is given this.parseTable(); // Parse a Google Spreadsheet this.parseGoogleSpreadsheet(); } }, /** * Get the column distribution. For example, a line series takes a single column for * Y values. A range series takes two columns for low and high values respectively, * and an OHLC series takes four columns. */ getColumnDistribution: function () { var chartOptions = this.chartOptions, getValueCount = function (type) { return (Highcharts.seriesTypes[type || 'line'].prototype.pointArrayMap || [0]).length; }, globalType = chartOptions && chartOptions.chart && chartOptions.chart.type, individualCounts = []; each((chartOptions && chartOptions.series) || [], function (series) { individualCounts.push(getValueCount(series.type || globalType)); }); this.valueCount = { global: getValueCount(globalType), individual: individualCounts }; }, dataFound: function () { // Interpret the values into right types this.parseTypes(); // Use first row for series names? this.findHeaderRow(); // Handle columns if a handleColumns callback is given this.parsed(); // Complete if a complete callback is given this.complete(); }, /** * Parse a CSV input string */ parseCSV: function () { var self = this, options = this.options, csv = options.csv, columns = this.columns, startRow = options.startRow || 0, endRow = options.endRow || Number.MAX_VALUE, startColumn = options.startColumn || 0, endColumn = options.endColumn || Number.MAX_VALUE, lines, activeRowNo = 0; if (csv) { lines = csv .replace(/\r /g, " ") // Unix .replace(/\r/g, " ") // Mac .split(options.lineDelimiter || " "); each(lines, function (line, rowNo) { var trimmed = self.trim(line), isComment = trimmed.indexOf('#') === 0, isBlank = trimmed === '', items; if (rowNo >= startRow && rowNo <= endRow && !isComment && !isBlank) { items = line.split(options.itemDelimiter || ','); each(items, function (item, colNo) { if (colNo >= startColumn && colNo <= endColumn) { if (!columns[colNo - startColumn]) { columns[colNo - startColumn] = []; } columns[colNo - startColumn][activeRowNo] = item; } }); activeRowNo += 1; } }); this.dataFound(); } }, /** * Parse a HTML table */ parseTable: function () { var options = this.options, table = options.table, columns = this.columns, startRow = options.startRow || 0, endRow = options.endRow || Number.MAX_VALUE, startColumn = options.startColumn || 0, endColumn = options.endColumn || Number.MAX_VALUE, colNo; if (table) { if (typeof table === 'string') { table = document.getElementById(table); } each(table.getElementsByTagName('tr'), function (tr, rowNo) { colNo = 0; if (rowNo >= startRow && rowNo <= endRow) { each(tr.childNodes, function (item) { if ((item.tagName === 'TD' || item.tagName === 'TH') && colNo >= startColumn && colNo <= endColumn) { if (!columns[colNo]) { columns[colNo] = []; } columns[colNo][rowNo - startRow] = item.innerHTML; colNo += 1; } }); } }); this.dataFound(); // continue } }, /** * TODO: * - switchRowsAndColumns */ parseGoogleSpreadsheet: function () { var self = this, options = this.options, googleSpreadsheetKey = options.googleSpreadsheetKey, columns = this.columns, startRow = options.startRow || 0, endRow = options.endRow || Number.MAX_VALUE, startColumn = options.startColumn || 0, endColumn = options.endColumn || Number.MAX_VALUE, gr, // google row gc; // google column if (googleSpreadsheetKey) { jQuery.getJSON('https://spreadsheets.google.com/feeds/cells/' + googleSpreadsheetKey + '/' + (options.googleSpreadsheetWorksheet || 'od6') + '/public/values?alt=json-in-script&callback=?', function (json) { // Prepare the data from the spreadsheat var cells = json.feed.entry, cell, cellCount = cells.length, colCount = 0, rowCount = 0, i; // First, find the total number of columns and rows that // are actually filled with data for (i = 0; i < cellCount; i++) { cell = cells[i]; colCount = Math.max(colCount, cell.gs$cell.col); rowCount = Math.max(rowCount, cell.gs$cell.row); } // Set up arrays containing the column data for (i = 0; i < colCount; i++) { if (i >= startColumn && i <= endColumn) { // Create new columns with the length of either end-start or rowCount columns[i - startColumn] = []; // Setting the length to avoid jslint warning columns[i - startColumn].length = Math.min(rowCount, endRow - startRow); } } // Loop over the cells and assign the value to the right // place in the column arrays for (i = 0; i < cellCount; i++) { cell = cells[i]; gr = cell.gs$cell.row - 1; // rows start at 1 gc = cell.gs$cell.col - 1; // columns start at 1 // If both row and col falls inside start and end // set the transposed cell value in the newly created columns if (gc >= startColumn && gc <= endColumn && gr >= startRow && gr <= endRow) { columns[gc - startColumn][gr - startRow] = cell.content.$t; } } self.dataFound(); }); } }, /** * Find the header row. For now, we just check whether the first row contains * numbers or strings. Later we could loop down and find the first row with * numbers. */ findHeaderRow: function () { var headerRow = 0; each(this.columns, function (column) { if (typeof column[0] !== 'string') { headerRow = null; } }); this.headerRow = 0; }, /** * Trim a string from whitespace */ trim: function (str) { return typeof str === 'string' ? str.replace(/^\s+|\s+$/g, '') : str; }, /** * Parse numeric cells in to number types and date types in to true dates. * @param {Object} columns */ parseTypes: function () { var columns = this.columns, col = columns.length, row, val, floatVal, trimVal, dateVal; while (col--) { row = columns[col].length; while (row--) { val = columns[col][row]; floatVal = parseFloat(val); trimVal = this.trim(val); /*jslint eqeq: true*/ if (trimVal == floatVal) { // is numeric /*jslint eqeq: false*/ columns[col][row] = floatVal; // If the number is greater than milliseconds in a year, assume datetime if (floatVal > 365 * 24 * 3600 * 1000) { columns[col].isDatetime = true; } else { columns[col].isNumeric = true; } } else { // string, continue to determine if it is a date string or really a string dateVal = this.parseDate(val); if (col === 0 && typeof dateVal === 'number' && !isNaN(dateVal)) { // is date columns[col][row] = dateVal; columns[col].isDatetime = true; } else { // string columns[col][row] = trimVal === '' ? null : trimVal; } } } } }, //* dateFormats: { 'YYYY-mm-dd': { regex: '^([0-9]{4})-([0-9]{2})-([0-9]{2})$', parser: function (match) { return Date.UTC(+match[1], match[2] - 1, +match[3]); } } }, // */ /** * Parse a date and return it as a number. Overridable through options.parseDate. */ parseDate: function (val) { var parseDate = this.options.parseDate, ret, key, format, match; if (parseDate) { ret = parseDate(val); } if (typeof val === 'string') { for (key in this.dateFormats) { format = this.dateFormats[key]; match = val.match(format.regex); if (match) { ret = format.parser(match); } } } return ret; }, /** * Reorganize rows into columns */ rowsToColumns: function (rows) { var row, rowsLength, col, colsLength, columns; if (rows) { columns = []; rowsLength = rows.length; for (row = 0; row < rowsLength; row++) { colsLength = rows[row].length; for (col = 0; col < colsLength; col++) { if (!columns[col]) { columns[col] = []; } columns[col][row] = rows[row][col]; } } } return columns; }, /** * A hook for working directly on the parsed columns */ parsed: function () { if (this.options.parsed) { this.options.parsed.call(this, this.columns); } }, /** * If a complete callback function is provided in the options, interpret the * columns into a Highcharts options object. */ complete: function () { var columns = this.columns, firstCol, type, options = this.options, valueCount, series, data, i, j, seriesIndex; if (options.complete) { this.getColumnDistribution(); // Use first column for X data or categories? if (columns.length > 1) { firstCol = columns.shift(); if (this.headerRow === 0) { firstCol.shift(); // remove the first cell } if (firstCol.isDatetime) { type = 'datetime'; } else if (!firstCol.isNumeric) { type = 'category'; } } // Get the names and shift the top row for (i = 0; i < columns.length; i++) { if (this.headerRow === 0) { columns[i].name = columns[i].shift(); } } // Use the next columns for series series = []; for (i = 0, seriesIndex = 0; i < columns.length; seriesIndex++) { // This series' value count valueCount = Highcharts.pick(this.valueCount.individual[seriesIndex], this.valueCount.global); // Iterate down the cells of each column and add data to the series data = []; for (j = 0; j < columns[i].length; j++) { data[j] = [ firstCol[j], columns[i][j] !== undefined ? columns[i][j] : null ]; if (valueCount > 1) { data[j].push(columns[i + 1][j] !== undefined ? columns[i + 1][j] : null); } if (valueCount > 2) { data[j].push(columns[i + 2][j] !== undefined ? columns[i + 2][j] : null); } if (valueCount > 3) { data[j].push(columns[i + 3][j] !== undefined ? columns[i + 3][j] : null); } if (valueCount > 4) { data[j].push(columns[i + 4][j] !== undefined ? columns[i + 4][j] : null); } } // Add the series series[seriesIndex] = { name: columns[i].name, data: data }; i += valueCount; } // Do the callback options.complete({ xAxis: { type: type }, series: series }); } } }); // Register the Data prototype and data function on Highcharts Highcharts.Data = Data; Highcharts.data = function (options, chartOptions) { return new Data(options, chartOptions); }; // Extend Chart.init so that the Chart constructor accepts a new configuration // option group, data. Highcharts.wrap(Highcharts.Chart.prototype, 'init', function (proceed, userOptions, callback) { var chart = this; if (userOptions && userOptions.data) { Highcharts.data(Highcharts.extend(userOptions.data, { complete: function (dataOptions) { // Merge series configs if (userOptions.series) { each(userOptions.series, function (series, i) { userOptions.series[i] = Highcharts.merge(series, dataOptions.series[i]); }); } // Do the merge userOptions = Highcharts.merge(dataOptions, userOptions); proceed.call(chart, userOptions, callback); } }), userOptions); } else { proceed.call(chart, userOptions, callback); } }); }(Highcharts)); |