2014-12-09 3 views
0

Я нашел обходное решение, чтобы нарисовать диаграмму gantt с линией высоких карт. Было приятно узнать, что мы можем составить диаграмму gantt с потрясающей библиотекой Highcharts, но я хочу применить пользовательские цвета. Возможно, я пробовал все возможности, которые я нашел в API. You can see what I did in my jsfiddleПрименение пользовательских цветов на линии HighCharts?

// Define applications 
 
var applications = [{ 
 
    name: 'App1', 
 
    intervals: [{ // From-To pairs 
 
     from: Date.UTC(2015, 0, 5), 
 
     to: Date.UTC(2015, 0, 6), 
 
     // We can't specify a single color for each data even we give an array of objects with named, intervals values and color. 
 
     color: '#FF4719' 
 
    }, { 
 
     from: Date.UTC(2015, 0, 12), 
 
     to: Date.UTC(2015, 0, 16) 
 
     ,color: '#2EB82E' 
 
    }] 
 
    // We can specify a single color of a line 
 
    //,color: '#2EB82E' 
 
}, { 
 
    name: 'App2', 
 
    intervals: [{ // From-To pairs 
 
     from: Date.UTC(2015, 0, 7), 
 
     to: Date.UTC(2015, 0, 9) 
 
     // We can't specify a single color for each data even we give an array of objects with named, intervals values and color. 
 
     ,color: '#FFFF19' 
 
    }, { 
 
     from: Date.UTC(2015, 0, 26), 
 
     to: Date.UTC(2015, 1, 6) 
 
     ,color: '#3366FF' 
 
    }] 
 
    // We can specify a single color of a line 
 
    //,color: '#3366FF' 
 
}, { 
 
    name: 'App3', 
 
    intervals: [{ // From-To pairs 
 
     from: Date.UTC(2015, 1, 20), 
 
     to: Date.UTC(2015, 1, 21), 
 
     label: 'Incident1' 
 
     // We can't specify a single color for each data even we give an array of objects with named, intervals values and color. 
 
     ,color: '#E62EB8' 
 
    }, { 
 
     from: Date.UTC(2015, 2,11), 
 
     to: Date.UTC(2015, 2, 13) 
 
     ,color: '#8A5C2E' 
 
    }, { 
 
     from: Date.UTC(2015, 2, 19), 
 
     to: Date.UTC(2015, 2, 20), 
 
     label: 'Incident2' 
 
     ,color: '#006699' 
 
    }, { 
 
     from: Date.UTC(2015, 2, 23), 
 
     to: Date.UTC(2015, 2, 27) 
 
     ,color: '#666699' 
 
    }] 
 
    // We can specify a single color of a line 
 
    //,color: '#666699' 
 
}, { 
 
    name: 'App4', 
 
    intervals: [{ // From-To pairs 
 
     from: Date.UTC(2015, 2, 2), 
 
     to: Date.UTC(2015, 2, 31) 
 
     // We can't specify a single color for each data even we give an array of objects with named, intervals values and color. 
 
     ,color: '#339966' 
 
    }] 
 
    //,color: '#339966' 
 
}]; 
 
// re-structure the applications into line seriesvar series = []; 
 
var series = []; 
 
$.each(applications.reverse(), function(i, application) { 
 
    var item = { 
 
     name: application.name, 
 
     data: [], 
 
     pointStart: Date.UTC(2015, 0, 1), 
 
     pointInterval: 3 * 24 * 3600 * 1000 
 
    }; 
 
    $.each(application.intervals, function(j, interval) { 
 
     item.data.push({ 
 
      x: interval.from, 
 
      y: i, 
 
      label: interval.label, 
 
      from: interval.from, 
 
      to: interval.to, 
 
      color: interval.color 
 
     }, { 
 
      x: interval.to, 
 
      y: i, 
 
      from: interval.from, 
 
      to: interval.to, 
 
      color: interval.color 
 
     }); 
 
     
 
     // add a null value between intervals 
 
     if (application.intervals[j + 1]) { 
 
      item.data.push(
 
       [(interval.to + application.intervals[j + 1].from)/2, null] 
 
      ); 
 
     } 
 

 
    }); 
 

 
    series.push(item); 
 
}); 
 

 
// create the chart 
 
var chart = new Highcharts.Chart({ 
 

 
    chart: { 
 
     renderTo: 'container' 
 
    }, 
 
    
 
    title: { 
 
     text: 'Deployment Planning' 
 
    }, 
 

 
    xAxis: { 
 
     //startOfWeek: 1, 
 
     type: 'datetime', 
 
     labels: { 
 
      formatter: function() { 
 
       return Highcharts.dateFormat('%e %b', this.value); 
 
      } 
 
     } 
 
    }, 
 

 
    yAxis: { 
 
     tickInterval: 1, 
 
     labels: { 
 
      formatter: function() { 
 
       if (applications[this.value]) { 
 
        return applications[this.value].name; 
 
       } 
 
      } 
 
     }, 
 
     startOnTick: false, 
 
     endOnTick: false, 
 
     title: { 
 
      text: 'Applications' 
 
     }, 
 
      minPadding: 0.2, 
 
       maxPadding: 0.2 
 
    }, 
 

 
    legend: { 
 
     enabled: false 
 
    }, 
 
    tooltip: { 
 
     formatter: function() { 
 
      return '<b>'+ applications[this.y].name + '</b><br/>' + 
 
       Highcharts.dateFormat('%Y-%m-%d', this.point.options.from) + 
 
       ' - ' + Highcharts.dateFormat('%Y-%m-%d', this.point.options.to); 
 
     } 
 
    }, 
 
    // We can define the color chart to our lines 
 
    //colors: ['#B572A7'], 
 
    plotOptions: { 
 
     series: { 
 
      // We can specify a single color of a line 
 
      //lineColor: '#303030' 
 
      //lineColor: function() { 
 
      //  return this.point.options.color; 
 
      //  return '#303030'; 
 
\t \t \t //}, 
 
     }, 
 
     line: { 
 
      lineWidth: 9, 
 
      // We can specify a single color of a line 
 
      //color: '#B572A7', 
 
      // We can't make function(){ ... } to get color for each  point.option or juste return a single color ! 
 
      //color: function() { 
 
      //  return this.point.options.color; 
 
      //  return '#B572A7'; 
 
\t \t \t //}, 
 
      marker: { 
 
       enabled: false 
 
      }, 
 
      dataLabels: { 
 
       enabled: true, 
 
       align: 'left', 
 
       formatter: function() { 
 
        return this.point.options && this.point.options.label; 
 
       } 
 
      } 
 
     } 
 
    }, 
 
    series: series 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="http://code.highcharts.com/highcharts.js"></script> 
 
<script src="http://code.highcharts.com/highcharts-more.js"></script> 
 
<div id="container" style="height: 180px"></div>

Я хочу, чтобы задать один цвет для каждого типа данных с массивом дает объекты с именем, значением интервалов и цветом. Как и рис

Like the pic

Любая идея, чтобы сделать это?

Thx

+0

вам понадобится отдельная серия для каждого цвета, который вы хотите. – jlbriggs

+0

Я не понимаю, вы можете объяснить больше PLZ? –

+0

Я вижу, что вы нашли другое решение, но для ясности: каждая линейка может быть только одного цвета. Таким образом, для каждой строки, которая нуждается в другом цвете, вам нужна отдельная серия для этой строки. – jlbriggs

ответ

0

Я нашел другой способ нарисовать диаграмму gantt с помощью highstock! Я использовал columnrange диаграмму А теперь я получаю то, что я хочу: D jsfiddle

Highcharts.setOptions({ 
 
\t lang: { 
 
\t \t months: ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'], 
 
\t \t weekdays: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'], 
 
\t \t shortMonths: ['Jan', 'Fev', 'Mar', 'Avr', 'Mai', 'Juin', 'Juil', 'Août', 'Sept', 'Oct', 'Nov', 'Déc'], 
 
\t \t decimalPoint: ',', 
 
\t \t printChart: 'Imprimer', 
 
\t \t downloadPNG: 'Télécharger en image PNG', 
 
\t \t downloadJPEG: 'Télécharger en image JPEG', 
 
\t \t downloadPDF: 'Télécharger en document PDF', 
 
\t \t downloadSVG: 'Télécharger en document Vectoriel', 
 
\t \t loading: 'Chargement en cours...', 
 
\t \t contextButtonTitle: 'Exporter le graphique', 
 
\t \t resetZoom: 'Réinitialiser le zoom', 
 
\t \t resetZoomTitle: 'Réinitialiser le zoom au niveau 1:1', 
 
\t \t thousandsSep: ' ', 
 
\t \t decimalPoint: ',', 
 
\t \t noData: 'Pas d\'information à afficher' 
 
\t } 
 
}); 
 
// Define applications 
 
var applications = [{ 
 
\t name: 'App1', 
 
\t intervals: [{ // From-To pairs 
 
\t \t from: Date.UTC(2015, 0, 5), 
 
\t \t to: Date.UTC(2015, 0, 6) 
 
\t \t ,step: '1' 
 
\t \t ,color: '#FF0000' 
 
\t }, { 
 
\t \t from: Date.UTC(2015, 0, 12), 
 
\t \t to: Date.UTC(2015, 0, 16) 
 
\t \t ,color: '#0066FF' 
 
\t \t ,step: '2' 
 
\t }, { 
 
\t \t from: Date.UTC(2015, 1, 12), 
 
\t \t to: Date.UTC(2015, 1, 16) 
 
\t \t ,color: '#00CC66' 
 
\t \t ,step: '3' 
 
\t }] 
 
}, { 
 
\t name: 'App2', 
 
\t intervals: [{ // From-To pairs 
 
\t \t from: Date.UTC(2015, 0, 7), 
 
\t \t to: Date.UTC(2015, 0, 9) 
 
\t \t ,step: '1' 
 
\t \t ,color: '#FF0000' 
 
\t }, { 
 
\t \t from: Date.UTC(2015, 0, 26), 
 
\t \t to: Date.UTC(2015, 1, 6) 
 
\t \t ,step: '2' 
 
\t \t ,color: '#0066FF' 
 
\t }] 
 
}, { 
 
\t name: 'App3', 
 
\t intervals: [{ // From-To pairs 
 
\t \t from: Date.UTC(2015, 1, 20), 
 
\t \t to: Date.UTC(2015, 1, 21), 
 
\t \t label: 'Incident1' 
 
\t \t ,step: '1' 
 
\t \t ,color: '#FF0000' 
 
\t }, { 
 
\t \t from: Date.UTC(2015, 2,11), 
 
\t \t to: Date.UTC(2015, 2, 13) 
 
\t \t ,step: '2' 
 
\t \t ,color: '#0066FF' 
 
\t }, { 
 
\t \t from: Date.UTC(2015, 2, 19), 
 
\t \t to: Date.UTC(2015, 2, 20), 
 
\t \t label: 'Incident2' 
 
\t \t ,step: '3' 
 
\t \t ,color: '#00CC66' 
 
\t }, { 
 
\t \t from: Date.UTC(2015, 2, 23), 
 
\t \t to: Date.UTC(2015, 2, 27) 
 
\t \t ,step: '4' 
 
\t \t ,color: '#A3CC29' 
 
\t }] 
 
}, { 
 
\t name: 'App4', 
 
\t intervals: [{ // From-To pairs 
 
\t \t from: Date.UTC(2015, 2, 2), 
 
\t \t to: Date.UTC(2015, 2, 31) 
 
\t \t ,step: '1' 
 
\t \t ,color: '#FF0000' 
 
\t }] 
 
}]; 
 
// re-structure the applications into line seriesvar series = []; 
 
var series = []; 
 
$.each(applications.reverse(), function(i, application) { 
 
\t var item = { 
 
\t \t name: application.name, 
 
\t \t data: [] 
 
\t }; 
 
\t $.each(application.intervals, function(j, interval) { 
 
\t \t item.data.push({ 
 
\t \t \t x: i, 
 
\t \t \t label: interval.label, 
 
\t \t \t low: interval.from, 
 
\t \t \t high: interval.to, 
 
\t \t \t step: interval.step, 
 
\t \t \t color: interval.color 
 
\t \t }, { 
 
\t \t \t x: i, 
 
\t \t \t low: interval.from, 
 
\t \t \t high: interval.to, 
 
\t \t \t step: interval.step, 
 
\t \t \t color: interval.color 
 
\t \t }); 
 
\t \t 
 
\t \t // add a null value between intervals 
 
\t \t if (application.intervals[j + 1]) { 
 
\t \t \t item.data.push(
 
\t \t \t \t [(interval.to + application.intervals[j + 1].from)/2, null] 
 
\t \t \t); 
 
\t \t } 
 

 
\t }); 
 

 
\t series.push(item); 
 
}); 
 
$('#container').highcharts({ 
 
\t chart: { 
 
\t \t type: 'columnrange', 
 
\t \t inverted: true, 
 
\t \t width: 800 
 
\t }, 
 
\t title: { 
 
\t \t text: 'Deployment Planning' 
 
\t }, 
 
\t xAxis: { 
 
\t \t categories: ['App4', 'App3', 'App2', 'App1'], 
 
\t \t title: { 
 
\t \t \t text: 'Applications' 
 
\t \t }, 
 
\t \t minPadding: 0.2, 
 
\t \t maxPadding: 0.2 
 
\t }, 
 
\t yAxis: { 
 
\t \t title: { 
 
\t \t \t text: '' 
 
\t \t }, 
 
\t \t type: 'datetime', 
 
\t \t dateTimeLabelFormats: { 
 
\t \t \t week: '%e %b' 
 
\t \t }, 
 
\t \t tickPixelInterval: 70, 
 
\t \t labels: { 
 
\t \t \t rotation: -45 
 
\t \t } 
 
\t }, 
 
\t plotOptions: { 
 
\t \t columnrange: { 
 
\t \t \t grouping: true, 
 
\t \t \t dataLabels: { 
 
\t \t \t \t enabled: false, 
 
\t \t \t \t align: 'center', 
 
\t \t \t \t formatter: function() { 
 
\t \t \t \t \t return this.point.options.label; 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t } 
 
\t }, 
 
\t legend: { 
 
\t \t enabled: false 
 
\t }, 
 
\t tooltip: { 
 
\t \t positioner: function() { 
 
\t \t \t return { x: 100, y: 35 }; 
 
\t \t }, 
 
\t \t formatter: function() { 
 
\t \t \t return this.point.options.step +" - "+ this.series.name + ' (' + Highcharts.dateFormat('%e %B', this.point.low) + 
 
\t \t \t \t ' - ' + Highcharts.dateFormat('%B %e', this.point.high) + ')'; 
 
\t \t } 
 
\t }, 
 
\t series: series 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="http://code.highcharts.com/stock/highstock.js"></script> 
 
<script src="http://code.highcharts.com/highcharts-more.js"></script> 
 
<div id="container" style="width: 100%; height: 400px;"></div>

0

Unfortunatley вы не можете установить различные цвета для одной серии (точки на линии).

Смежные вопросы