javascript - Plotting Global variable Highcharts -
after many failed attempts, thought let smarter me correct me. creating simple chart using highcharts. data retrieved once (php code below) using $.post method. in console see formatted. yet, cannot highcharts display it. have tried many different ways variety of posts still nothing.
everything works fine if use $.getjson , create chart. however, end user go , forth many time review same data. not want query every time. idea place data on global variable using data generate chart.
here php:
<?php require_once "../includes/config_rev.php"; require_once "../includes/connect.php"; global $db; $st = $db->prepare("select * mdo"); $st->execute(); $category = array(); $category['name'] = 'mes'; $series1 = array(); $series1['name'] = 'temporeros'; $series2 = array(); $series2['name'] = 'planta'; $series3 = array(); $series3['name'] = 'movilizacion'; foreach ($st $stmenuselection) { $category['data'][] = $stmenuselection['mes']; $series1['data'][] = $stmenuselection['temporeros']; $series2['data'][] = $stmenuselection['planta']; $series3['data'][] = $stmenuselection['movilizacion']; } $results = array(); array_push($results,$category); array_push($results,$series1); array_push($results,$series2); array_push($results,$series3); $json = json_encode($results, json_numeric_check); echo $json; $db = null; ?>
here js creates chart:
var mdo; $(document).ready(function() { $.post('_presu_mdo.php', function ( data ) {mdo = data}); }); function manodeobra() { var options = { chart: { renderto: 'mdo', type: 'bar' }, title: { text: 'presupuesto de mano de obra', x: -20 //center }, subtitle: { text: '', x: -20 }, xaxis: { categories: mdo[0] }, yaxis: { min: 0, title: { text: 'requests' }, labels: { overflow: 'justify' } }, tooltip: { formatter: function() { return '<b>'+ this.series.name +'</b><br/>'+ this.x +': '+ this.y; } }, legend: { layout: 'vertical', align: 'right', verticalalign: 'top', x: -10, y: 100, borderwidth: 0 }, plotoptions: { bar: { datalabels: { enabled: true } } }, series: [] } options.xaxis.categories = mdo[0]['mes']; options.series[0] = mdo[1]['temporeros']; options.series[1] = mdo[2]['planta']; options.series[2] = mdo[3]['movilizacion']; chart = new highcharts.chart(options); }
how can function manodeobra() draw chart using mdo variable without having again run query?
thanks in advance,
i see number of potential problems:
1.) when call manodeobra
function? since $.post
async need make sure don't call until after completes.
2.) state ajax looks array of objects:
[ { "name": "mes", "data": [ "enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre" ] }, { "name": "temporeros", "data": [ 22000000, 17000000, 18000000 ] }, { "name": "planta", "data": [ 15000000, 15000000, 15000000, <-- ie won't this! ] }, { "name": "movilizacion", "data": [ 2000000, 2000000, 2000000 ] } ]
the dangling comma in planta
invalid ie.
also have:
options.xaxis.categories = mdo[0]['mes']; options.series[0] = mdo[1]['temporeros']; options.series[1] = mdo[2]['planta']; options.series[2] = mdo[3]['movilizacion'];
these properties on each array item don't exist in json. also, series attribute in options object empty array, .series[1]
, .series[2]
invalid.
i suspect need:
options.xaxis.categories = mdo[0]['data'] options.series.push(mdo[1]); options.series.push(mdo[2]); options.series.push(mdo[3]);
3.) learn inspect javascript console errors (in ie or chrome press f12). of these problems evident if checked errors.
Comments
Post a Comment