2016-05-28 2 views
0

Я работаю с реакциями с высоким разрешением. Он отлично работает, кроме состояния nodata. Мне нужно отобразить сообщение «Нет данных», когда у диаграммы есть пустые данные.Сообщение Nodata с React 0.13.3 и high-high-rate 3.0.0

У меня есть проверка no-data-to-display.js официальных высокочастотных диаграмм, но она не работает с React. Я хотел бы сделать результат, как этот: 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; 

Я использую react 0.13.3, react-highcharts версии 3.0.0 и highcharts-no-data-to-display версии 0.1.2

+1

Вы пробовали использовать [решение официальных Highcharts для ReactJS] (HTTP://www.highcharts.com/blog/192-use-highcharts-to-create-charts-in-react)? Существует пример использования модулей с Highcharts. –

ответ

0

https://github.com/kirjs/react-highcharts
советы: Использование Highcharts модулей/дополнение, такие как экспортирующие, данные и т.д. (демо)

например:

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

Добавление объяснения тому, как этот код отвечает на вопрос, поможет будущим посетителям. – JAL

+0

При первом ответе возникла ошибка: код ошибки: импортировать Highcharts из «response-highcharts/dist/bundle/highcharts»; требуют («highcharts-no-data-to-display») (ReactHighcharts.Highcharts); правильный код: импортировать Highcharts из «реакций-highcharts/dist/bundle/highcharts»; require ('highcharts-no-data-to-display') (Highcharts.Highcharts) – succpeking

+0

Я чувствую, что редактор потока потока в стеке очень сулит. Я думаю, что github очень классный, может быть, вы это и думаете. – succpeking

0

UPDATE: Я создал пакет НПМ для этого, просто установите его, и использовать его! это один -> react-highcharts-no-data-to-display

ОТВЕТ: То, что вы должны сделать, это добавить (ReactHighcharts.Highcharts) рядом с require('highcharts-no-data-to-display') довольно легко, верно?

На всякий случай, если кто-то сталкивается с той же проблемой (пытается добавить сообщение «нет данных» в React-HighCharts). Этапы:

  1. Установите его! в терминальном периоде: npm install highcharts-no-data-to-display --save
  2. В среагировать файл, который имеет диаграмму, которую вы хотите добавить сообщение нет-данных, вы должны добавить require('highcharts-no-data-to-display')(ReactHighcharts.Highcharts) на первых строках
  3. Кроме того, если вы хотите, чтобы настроить текст и позиции сообщения. Добавьте это:

>

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

полный код, что @ittus спрашивает должен быть

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; 
Смежные вопросы