2016-05-28 1 views
0

Je travaille avec reac-highcharts. Cela fonctionne parfaitement sauf l'état nodata. Je dois afficher le message «Pas de données disponibles» lorsque le graphique contient des données vides.Message Nodata avec React 0.13.3 et reactif-highcharts 3.0.0

J'ai coché no-data-to-display.js de highcharts officiels mais cela ne fonctionne pas avec React. Je voudrais faire un résultat comme celui-ci: http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/no-data-to-display/no-data-pie/

import React from 'react'; 
import Highcharts from 'react-highcharts/dist/bundle/highcharts'; 
require('highcharts-no-data-to-display'); 

class MyChart extends React.Component { 
    constructor(props) { 
     super(); 
     this.state = this._getInitialState(props); 
    } 
    static chartColors() { 
     return [ 
     '#04a5af', '#4a6cb4', '#2d4665', '#76b5db', '#b4dcee','#cae9de','#24a9b2','#48d0ae','#2a2b32', '#5065ae' 
     ] 
    } 

    componentWillReceiveProps(newProps) { 
     this.setState(this._getInitialState(newProps)); 
    } 

    _getInitialState(props) { 
     return { 
      chartConfig: 
      { 
       colors: MyChart.chartColors(), 
       chart: { 
        type: 'column', 
        events: { 
         load: function(event) { 
          event.target.reflow(); 
         } 
        } 
       }, 
       credits: { 
        enabled: false 
       }, 
       title: { 
        text: props.title 
       }, 
       xAxis: { 
        type: 'datetime', 
        title: { 
         text: '', 
         style: { 
          fontSize: '12px' 
         } 
        }, 
        labels:{ 
         style:{ 
          fontSize: '12px' 
         } 
        }, 
        dateTimeLabelFormats : { 
         second : '%H:%M', 
         minute : '%H:%M', 
         hour : '%H:%M', 
         day : '%e-$b-%y', 
         week : '%e', 
         month : '%e', 
         year : '%e' 
        }, 
        alternateGridColor: '#FAFAFA', 
        startOnTick: true, 
        endOnTick: true, 
        categories: [], 
       }, 
       yAxis: { 
        min: 0, 
        title: { 
         text: props.yTitle?props.yTitle: "" 
        }, 
        stackLabels: { 
         enabled: false, 
         style: { 
          fontWeight: 'bold', 
          color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray' 
         } 
        } 
       }, 
       legend: { 
        align: 'center', 
        y: 15, 
        floating: false, 
        backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || 'white', 
        shadow: false 
       }, 
       tooltip: { 
        pointFormat: '{series.name}: {point.y}<br/>Total: {point.stackTotal}' 
       }, 
       plotOptions: { 
        column: { 
         stacking: 'normal', 
         dataLabels: { 
          enabled: false, 
          color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white', 
          style: { 
           textShadow: '0 0 3px black' 
          } 
         } 
        } 
       }, 
       noData: { 
         position: { 
          "x": 0, 
          "y": 0, 
          "align": "center", 
          "verticalAlign": "middle" 
         } 
       }, 
       series: props.series 


      } 

     }; 

    } 



    render() { 


     return (
      <div refs="wa-chart"> 
       <Highcharts config={this.state.chartConfig} ref="chart" isPureConfig={true} /> 
      </div>); 
    } 
} 



export default MyChart; 

J'utilise react 0.13.3, react-highcharts la version 3.0.0 et la version highcharts-no-data-to-display 0.1.2

+1

Avez-vous essayé d'utiliser [la solution Highcharts officielle pour ReactJS] (http://www.highcharts.com/blog/192-use-highcharts-to-create-charts-in-react)? Il y a un exemple d'utilisation de modules avec des graphiques hauts. –

Répondre

0

https://github.com/kirjs/react-highcharts
Conseils: L'utilisation des modules de Highcharts/add-ons comme l'exportation, les données, etc. (demo)

par exemple:

import React from 'react'; 
 
import Highcharts from 'react-highcharts/dist/bundle/highcharts'; 
 
require('highcharts-no-data-to-display')(ReactHighcharts.Highcharts) 
 

 
class MyChart extends React.Component { 
 
    constructor(props) { 
 
     super(); 
 
     this.state = this._getInitialState(props); 
 
    } 
 
    static chartColors() { 
 
     return [ 
 
     '#04a5af', '#4a6cb4', '#2d4665', '#76b5db', '#b4dcee','#cae9de','#24a9b2','#48d0ae','#2a2b32', '#5065ae' 
 
     ] 
 
    } 
 

 
    componentWillReceiveProps(newProps) { 
 
     this.setState(this._getInitialState(newProps)); 
 
    } 
 

 
    _getInitialState(props) { 
 
     return { 
 
      chartConfig: 
 
      { 
 
       colors: MyChart.chartColors(), 
 
       chart: { 
 
        type: 'column', 
 
        events: { 
 
         load: function(event) { 
 
          event.target.reflow(); 
 
         } 
 
        } 
 
       }, 
 
       credits: { 
 
        enabled: false 
 
       }, 
 
       title: { 
 
        text: props.title 
 
       }, 
 
       xAxis: { 
 
        type: 'datetime', 
 
        title: { 
 
         text: '', 
 
         style: { 
 
          fontSize: '12px' 
 
         } 
 
        }, 
 
        labels:{ 
 
         style:{ 
 
          fontSize: '12px' 
 
         } 
 
        }, 
 
        dateTimeLabelFormats : { 
 
         second : '%H:%M', 
 
         minute : '%H:%M', 
 
         hour : '%H:%M', 
 
         day : '%e-$b-%y', 
 
         week : '%e', 
 
         month : '%e', 
 
         year : '%e' 
 
        }, 
 
        alternateGridColor: '#FAFAFA', 
 
        startOnTick: true, 
 
        endOnTick: true, 
 
        categories: [], 
 
       }, 
 
       yAxis: { 
 
        min: 0, 
 
        title: { 
 
         text: props.yTitle?props.yTitle: "" 
 
        }, 
 
        stackLabels: { 
 
         enabled: false, 
 
         style: { 
 
          fontWeight: 'bold', 
 
          color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray' 
 
         } 
 
        } 
 
       }, 
 
       legend: { 
 
        align: 'center', 
 
        y: 15, 
 
        floating: false, 
 
        backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || 'white', 
 
        shadow: false 
 
       }, 
 
       tooltip: { 
 
        pointFormat: '{series.name}: {point.y}<br/>Total: {point.stackTotal}' 
 
       }, 
 
       plotOptions: { 
 
        column: { 
 
         stacking: 'normal', 
 
         dataLabels: { 
 
          enabled: false, 
 
          color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white', 
 
          style: { 
 
           textShadow: '0 0 3px black' 
 
          } 
 
         } 
 
        } 
 
       }, 
 
       lang:{ 
 
        noData: 'no data!' 
 
       }, 
 
       noData: { 
 
         position: { 
 
          "x": 0, 
 
          "y": 0, 
 
          "align": "center", 
 
          "verticalAlign": "middle" 
 
         } 
 
       }, 
 
       series: props.series 
 

 

 
      } 
 

 
     }; 
 

 
    } 
 

 

 

 
    render() { 
 

 

 
     return (
 
      <div refs="wa-chart"> 
 
       <Highcharts config={this.state.chartConfig} ref="chart" isPureConfig={true} /> 
 
      </div>); 
 
    } 
 
} 
 

 

 

 
export default MyChart;

0

import React from 'react'; 
 
import Highcharts from 'react-highcharts/dist/bundle/highcharts'; 
 
require('highcharts-no-data-to-display')(Highcharts.Highcharts) 
 

 
class MyChart extends React.Component { 
 
    constructor(props) { 
 
     super(); 
 
     this.state = this._getInitialState(props); 
 
    } 
 
    static chartColors() { 
 
     return [ 
 
     '#04a5af', '#4a6cb4', '#2d4665', '#76b5db', '#b4dcee','#cae9de','#24a9b2','#48d0ae','#2a2b32', '#5065ae' 
 
     ] 
 
    } 
 

 
    componentWillReceiveProps(newProps) { 
 
     this.setState(this._getInitialState(newProps)); 
 
    } 
 

 
    _getInitialState(props) { 
 
     return { 
 
      chartConfig: 
 
      { 
 
       colors: MyChart.chartColors(), 
 
       chart: { 
 
        type: 'column', 
 
        events: { 
 
         load: function(event) { 
 
          event.target.reflow(); 
 
         } 
 
        } 
 
       }, 
 
       credits: { 
 
        enabled: false 
 
       }, 
 
       title: { 
 
        text: props.title 
 
       }, 
 
       xAxis: { 
 
        type: 'datetime', 
 
        title: { 
 
         text: '', 
 
         style: { 
 
          fontSize: '12px' 
 
         } 
 
        }, 
 
        labels:{ 
 
         style:{ 
 
          fontSize: '12px' 
 
         } 
 
        }, 
 
        dateTimeLabelFormats : { 
 
         second : '%H:%M', 
 
         minute : '%H:%M', 
 
         hour : '%H:%M', 
 
         day : '%e-$b-%y', 
 
         week : '%e', 
 
         month : '%e', 
 
         year : '%e' 
 
        }, 
 
        alternateGridColor: '#FAFAFA', 
 
        startOnTick: true, 
 
        endOnTick: true, 
 
        categories: [], 
 
       }, 
 
       yAxis: { 
 
        min: 0, 
 
        title: { 
 
         text: props.yTitle?props.yTitle: "" 
 
        }, 
 
        stackLabels: { 
 
         enabled: false, 
 
         style: { 
 
          fontWeight: 'bold', 
 
          color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray' 
 
         } 
 
        } 
 
       }, 
 
       legend: { 
 
        align: 'center', 
 
        y: 15, 
 
        floating: false, 
 
        backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || 'white', 
 
        shadow: false 
 
       }, 
 
       tooltip: { 
 
        pointFormat: '{series.name}: {point.y}<br/>Total: {point.stackTotal}' 
 
       }, 
 
       plotOptions: { 
 
        column: { 
 
         stacking: 'normal', 
 
         dataLabels: { 
 
          enabled: false, 
 
          color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white', 
 
          style: { 
 
           textShadow: '0 0 3px black' 
 
          } 
 
         } 
 
        } 
 
       }, 
 
       lang:{ 
 
        noData: 'no data!' 
 
       }, 
 
       noData: { 
 
         position: { 
 
          "x": 0, 
 
          "y": 0, 
 
          "align": "center", 
 
          "verticalAlign": "middle" 
 
         } 
 
       }, 
 
       series: props.series 
 

 

 
      } 
 

 
     }; 
 

 
    } 
 

 

 

 
    render() { 
 

 

 
     return (
 
      <div refs="wa-chart"> 
 
       <Highcharts config={this.state.chartConfig} ref="chart" isPureConfig={true} /> 
 
      </div>); 
 
    } 
 
} 
 

 

 

 
export default MyChart;

+0

Ajouter une explication à la façon dont ce code répond à la question aidera les futurs visiteurs. – JAL

+0

Une erreur s'est produite lors de la première réponse, code d'erreur: importer des graphiques en hauteur à partir de 'react-highcharts/dist/bundle/highcharts'; require ('highcharts-no-data-to-display') (ReactHighcharts.Highcharts); code à droite: importer des graphiques à partir de 'reactif-highcharts/dist/bundle/highcharts'; require ('highcharts-no-data-to-display') (Highcharts.Highcharts) – succpeking

+0

Je pense que l'expérience de l'éditeur de flux de stackover est très boudeuse. Je pense que github est très cool, peut-être que vous le pensez. – succpeking

0

MISE À JOUR: J'ai créé un paquet NPM pour cela, il suffit de l'installer et l'utiliser! celui-ci -> react-highcharts-no-data-to-display

REPONSE: Ce que vous devez faire est d'ajouter (ReactHighcharts.Highcharts) côté droit require('highcharts-no-data-to-display') assez facile? Juste au cas où quelqu'un est confronté au même problème (en essayant d'ajouter le message "pas de données" dans React-HighCharts). Les étapes sont:

  1. Installez-le! dans une course terminal: npm install highcharts-no-data-to-display --save
  2. Dans REACT fichier qui a le graphique que vous voulez ajouter le message pas de données, vous devez ajouter require('highcharts-no-data-to-display')(ReactHighcharts.Highcharts) sur les premières lignes
  3. De plus, si vous souhaitez personnaliser le texte et position du message. Ajouter ceci:

>

lang:{ 
      noData: 'no data!' //the text to be displayed 
      }, 
      noData: { 
        position: { 
         "x": 0, 
         "y": 0, 
         "align": "center", 
         "verticalAlign": "middle" 
        } 
      } 

le code complet de ce @ittus demande devrait être

import React from 'react'; 
import Highcharts from 'react-highcharts/dist/bundle/highcharts'; 
require('highcharts-no-data-to-display')(Highcharts.Highcharts) 

class MyChart extends React.Component { 
    constructor(props) { 
     super(); 
     this.state = this._getInitialState(props); 
    } 
    static chartColors() { 
     return [ 
     '#04a5af', '#4a6cb4', '#2d4665', '#76b5db', '#b4dcee','#cae9de','#24a9b2','#48d0ae','#2a2b32', '#5065ae' 
     ] 
    } 

    componentWillReceiveProps(newProps) { 
     this.setState(this._getInitialState(newProps)); 
    } 

    _getInitialState(props) { 
     return { 
      chartConfig: 
      { 
       colors: MyChart.chartColors(), 
       chart: { 
        type: 'column', 
        events: { 
         load: function(event) { 
          event.target.reflow(); 
         } 
        } 
       }, 
       credits: { 
        enabled: false 
       }, 
       title: { 
        text: props.title 
       }, 
       xAxis: { 
        type: 'datetime', 
        title: { 
         text: '', 
         style: { 
          fontSize: '12px' 
         } 
        }, 
        labels:{ 
         style:{ 
          fontSize: '12px' 
         } 
        }, 
        dateTimeLabelFormats : { 
         second : '%H:%M', 
         minute : '%H:%M', 
         hour : '%H:%M', 
         day : '%e-$b-%y', 
         week : '%e', 
         month : '%e', 
         year : '%e' 
        }, 
        alternateGridColor: '#FAFAFA', 
        startOnTick: true, 
        endOnTick: true, 
        categories: [], 
       }, 
       yAxis: { 
        min: 0, 
        title: { 
         text: props.yTitle?props.yTitle: "" 
        }, 
        stackLabels: { 
         enabled: false, 
         style: { 
          fontWeight: 'bold', 
          color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray' 
         } 
        } 
       }, 
       legend: { 
        align: 'center', 
        y: 15, 
        floating: false, 
        backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || 'white', 
        shadow: false 
       }, 
       tooltip: { 
        pointFormat: '{series.name}: {point.y}<br/>Total: {point.stackTotal}' 
       }, 
       plotOptions: { 
        column: { 
         stacking: 'normal', 
         dataLabels: { 
          enabled: false, 
          color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white', 
          style: { 
           textShadow: '0 0 3px black' 
          } 
         } 
        } 
       }, 
       lang:{ 
        noData: 'no data!' 
       }, 
       noData: { 
         position: { 
          "x": 0, 
          "y": 0, 
          "align": "center", 
          "verticalAlign": "middle" 
         } 
       }, 
       series: props.series 


      } 

     }; 

    } 



    render() { 


     return (
      <div refs="wa-chart"> 
       <Highcharts config={this.state.chartConfig} ref="chart" isPureConfig={true} /> 
      </div>); 
    } 
} 



export default MyChart;