2017-07-26 61 views
3

我想更改庫存圖表中圖表的顏色。底部面板中有多個圖表。這些圖表應該有不同的顏色。更改AmCharts庫存圖表中的圖表顏色

片段是在這裏:

var chartData = generateChartData(); 
 

 
function generateChartData() { 
 
    var chartData = []; 
 
    var firstDate = new Date(2012, 0, 1); 
 
    firstDate.setDate(firstDate.getDate() - 1000); 
 
    firstDate.setHours(0, 0, 0, 0); 
 

 
    for (var i = 0; i < 1000; i++) { 
 
     var newDate = new Date(firstDate); 
 
     newDate.setHours(0, i, 0, 0); 
 

 
     var a = Math.round(Math.random() * (40 + i)) + 100 + i; 
 
     var b = Math.round(Math.random() * (40 + i)) + 100 + i; 
 

 
     chartData.push({ 
 
      "date": newDate, 
 
      "value": a, 
 
      "volume": b 
 
     }); 
 
    } 
 
    return chartData; 
 
} 
 

 
var chart = AmCharts.makeChart("chartdiv", { 
 
    "type": "stock", 
 
    "theme": "light", 
 
    "categoryAxesSettings": { 
 
     "minPeriod": "mm" 
 
    }, 
 
    "dataSets": [{ 
 
     "color":"red", //it changes the color of all the graphs, i need different color for bottom panel. 
 
     "fieldMappings": [{ 
 
      "fromField": "value", 
 
      "toField": "value" 
 
     }, { 
 
      "fromField": "volume", 
 
      "toField": "volume" 
 
     }], 
 
     "dataProvider": chartData, 
 
     "categoryField": "date" 
 
    }], 
 
    "panels": [{ 
 
     "showCategoryAxis": false, 
 
     "title": "Value", 
 
     "percentHeight": 70, 
 
     "stockGraphs": [{ 
 
     \t \t "fillAlphas": 0, 
 
\t \t \t \t \t \t "fillColors":"red", 
 
      "id": "g1", 
 
      "valueField": "value", 
 
      "type": "smoothedLine", 
 
      "lineThickness": 2, 
 
      "bullet": "round", 
 
      
 
     }], 
 
     "stockLegend": { 
 
      "valueTextRegular": " ", 
 
      "markerType": "none" 
 
     } 
 
    }, 
 
    { 
 
     "title": "Volume", 
 
     "percentHeight": 30, 
 
     "valueAxes": [{ 
 
      "id": "ValueAxis-1", 
 
      "title": "Axis title" 
 
     }], 
 
     "stockGraphs": [{ 
 
       "valueField": "volume", 
 
       "type": "column", 
 
       "cornerRadiusTop": 2, 
 
       "fillAlphas": 1, 
 
         "fillColorsField":"red" 
 
      }, 
 
      { 
 
       "valueField": "value", 
 
       //"type": "column", 
 
       "cornerRadiusTop": 5,"fillColorsField":"red" 
 
      } 
 
     ], 
 
     "stockLegend": { 
 
      "valueTextRegular": " ", 
 
      "markerType": "none" 
 
     } 
 
    }], 
 
    "chartScrollbarSettings": { 
 
     "graph": "g1", 
 
     "usePeriod": "10mm", 
 
     "position": "top" 
 
    }, 
 

 
    "chartCursorSettings": { 
 
     "valueBalloonsEnabled": true 
 
    }, 
 

 
    "periodSelector": { 
 
     "position": "top", 
 
     "dateFormat": "YYYY-MM-DD JJ:NN", 
 
     "inputFieldWidth": 150, 
 
     "periods": [{ 
 
      "period": "hh", 
 
      "count": 1, 
 
      "label": "1 hour", 
 
      "selected": true 
 
     }, { 
 
      "period": "hh", 
 
      "count": 2, 
 
      "label": "2 hours" 
 
     }, { 
 
      "period": "hh", 
 
      "count": 5, 
 
      "label": "5 hour" 
 
     }, { 
 
      "period": "hh", 
 
      "count": 12, 
 
      "label": "12 hours" 
 
     }, { 
 
      "period": "MAX", 
 
      "label": "MAX" 
 
     }] 
 
    }, 
 

 
    "panelsSettings": { 
 
     "usePrefixes": true 
 
    }, 
 

 
    "export": { 
 
     "enabled": true, 
 
     "position": "bottom-right" 
 
    } 
 
});
#chartdiv { 
 
    width: 100%; 
 
    height: 400px; 
 
}
<script src="https://www.amcharts.com/lib/3/amcharts.js"></script> 
 
<script src="https://www.amcharts.com/lib/3/serial.js"></script> 
 
<script src="https://www.amcharts.com/lib/3/amstock.js"></script> 
 
<script src="https://www.amcharts.com/lib/3/plugins/export/export.min.js"></script> 
 
<link rel="stylesheet" href="https://www.amcharts.com/lib/3/plugins/export/export.css" type="text/css" media="all" /> 
 
<script src="https://www.amcharts.com/lib/3/themes/light.js"></script> 
 
<div id="chartdiv"></div> \t \t \t \t \t \t \t \t \t \t

如果我的數據集變色那麼它改變了所有的圖表顏色。我想給他們不同的顏色。底部面板中有兩張圖。一個是專欄,另一個是專線。他們也應該有不同的顏色。

我嘗試了amCharts文檔中給出的所有顏色屬性,但沒有任何工作(Ref.)。如何做這件事?

+0

沒有ü看看這個:http://www.amcharts.com/demos/line-with-changing-color/ – Hash

回答

4

您可以設置useDataSetColors假每個stockGraph使它使每個圖都有不同的顏色。如果將此設置爲false,則您還可以通過自己的fillColorslineColor直接設置圖形對象內部的顏色及其關聯的* colorField屬性(如果您的數據中已定義顏色)。

stockGraphs: [{ 
    useDataSetcolors: false, //set for each stockGraph object where you don't 
          //want the graph to use the dataSet color and generate its own. 
    // ... 
}, { 
    useDataSetcolors: false, 
}] 

更新演示:

var chartData = generateChartData(); 
 

 
function generateChartData() { 
 
    var chartData = []; 
 
    var firstDate = new Date(2012, 0, 1); 
 
    firstDate.setDate(firstDate.getDate() - 1000); 
 
    firstDate.setHours(0, 0, 0, 0); 
 

 
    for (var i = 0; i < 1000; i++) { 
 
    var newDate = new Date(firstDate); 
 
    newDate.setHours(0, i, 0, 0); 
 

 
    var a = Math.round(Math.random() * (40 + i)) + 100 + i; 
 
    var b = Math.round(Math.random() * (40 + i)) + 100 + i; 
 

 
    chartData.push({ 
 
     "date": newDate, 
 
     "value": a, 
 
     "volume": b 
 
    }); 
 
    } 
 
    return chartData; 
 
} 
 

 
var chart = AmCharts.makeChart("chartdiv", { 
 
    "type": "stock", 
 
    "theme": "light", 
 
    "categoryAxesSettings": { 
 
    "minPeriod": "mm" 
 
    }, 
 
    "dataSets": [{ 
 
    "color": "red", 
 
    "fieldMappings": [{ 
 
     "fromField": "value", 
 
     "toField": "value" 
 
    }, { 
 
     "fromField": "volume", 
 
     "toField": "volume" 
 
    }], 
 
    "dataProvider": chartData, 
 
    "categoryField": "date" 
 
    }], 
 
    "panels": [{ 
 
     "showCategoryAxis": false, 
 
     "title": "Value", 
 
     "percentHeight": 70, 
 
     "stockGraphs": [{ 
 
     "fillAlphas": 0, 
 
     "fillColors": "red", 
 
     "id": "g1", 
 
     "valueField": "value", 
 
     "type": "smoothedLine", 
 
     "lineThickness": 2, 
 
     "bullet": "round", 
 

 
     }], 
 
     "stockLegend": { 
 
     "valueTextRegular": " ", 
 
     "markerType": "none" 
 
     } 
 
    }, 
 
    { 
 
     "title": "Volume", 
 
     "percentHeight": 30, 
 
     "valueAxes": [{ 
 
     "id": "ValueAxis-1", 
 
     "title": "Axis title" 
 
     }], 
 
     "stockGraphs": [{ 
 
      "useDataSetColors": false, 
 
      "valueField": "volume", 
 
      "type": "column", 
 
      "cornerRadiusTop": 2, 
 
      "fillAlphas": 1, 
 
      "fillColorsField": "red" //note that this looks for colors in your data for eacc point. use fillColors instead if you want to change the color directly for all points 
 
     }, 
 
     { 
 
      "useDataSetColors": false, 
 
      "valueField": "value", 
 
      //"type": "column", 
 
      "cornerRadiusTop": 5, 
 
      "fillColorsField": "red" 
 
     } 
 
     ], 
 
     "stockLegend": { 
 
     "valueTextRegular": " ", 
 
     "markerType": "none" 
 
     } 
 
    } 
 
    ], 
 
    "chartScrollbarSettings": { 
 
    "graph": "g1", 
 
    "usePeriod": "10mm", 
 
    "position": "top" 
 
    }, 
 

 
    "chartCursorSettings": { 
 
    "valueBalloonsEnabled": true 
 
    }, 
 

 
    "periodSelector": { 
 
    "position": "top", 
 
    "dateFormat": "YYYY-MM-DD JJ:NN", 
 
    "inputFieldWidth": 150, 
 
    "periods": [{ 
 
     "period": "hh", 
 
     "count": 1, 
 
     "label": "1 hour", 
 
     "selected": true 
 
    }, { 
 
     "period": "hh", 
 
     "count": 2, 
 
     "label": "2 hours" 
 
    }, { 
 
     "period": "hh", 
 
     "count": 5, 
 
     "label": "5 hour" 
 
    }, { 
 
     "period": "hh", 
 
     "count": 12, 
 
     "label": "12 hours" 
 
    }, { 
 
     "period": "MAX", 
 
     "label": "MAX" 
 
    }] 
 
    }, 
 

 
    "panelsSettings": { 
 
    "usePrefixes": true 
 
    }, 
 

 
    "export": { 
 
    "enabled": true, 
 
    "position": "bottom-right" 
 
    } 
 
});
#chartdiv { 
 
    width: 100%; 
 
    height: 400px; 
 
}
<script src="https://www.amcharts.com/lib/3/amcharts.js"></script> 
 
<script src="https://www.amcharts.com/lib/3/serial.js"></script> 
 
<script src="https://www.amcharts.com/lib/3/amstock.js"></script> 
 
<script src="https://www.amcharts.com/lib/3/plugins/export/export.min.js"></script> 
 
<link rel="stylesheet" href="https://www.amcharts.com/lib/3/plugins/export/export.css" type="text/css" media="all" /> 
 
<script src="https://www.amcharts.com/lib/3/themes/light.js"></script> 
 
<div id="chartdiv"></div>

+0

這就是我一直在尋找的。再次感謝@xorspark –

1

var chartData = generateChartData(); 
 

 
function generateChartData() { 
 
    var chartData = []; 
 
    var firstDate = new Date(2012, 0, 1); 
 
    firstDate.setDate(firstDate.getDate() - 1000); 
 
    firstDate.setHours(0, 0, 0, 0); 
 

 
    for (var i = 0; i < 1000; i++) { 
 
     var newDate = new Date(firstDate); 
 
     newDate.setHours(0, i, 0, 0); 
 

 
     var a = Math.round(Math.random() * (40 + i)) + 100 + i; 
 
     var b = Math.round(Math.random() * (40 + i)) + 100 + i; 
 

 
     chartData.push({ 
 
      "date": newDate, 
 
      "value": a, 
 
      "volume": b 
 
     }); 
 
    } 
 
    return chartData; 
 
} 
 

 
var chart = AmCharts.makeChart("chartdiv", { 
 
    "type": "stock", 
 
    "theme": "light", 
 
    "categoryAxesSettings": { 
 
     "minPeriod": "mm" 
 
    }, 
 
    "dataSets": [{ 
 
     "color":"red", //it changes the color of all the graphs, i need different color for bottom panel. 
 
     "fieldMappings": [{ 
 
      "fromField": "value", 
 
      "toField": "value" 
 
     }, { 
 
      "fromField": "volume", 
 
      "toField": "volume" 
 
     }], 
 
     "dataProvider": chartData, 
 
     "categoryField": "date" 
 
    }], 
 
    "panels": [{ 
 
     "showCategoryAxis": false, 
 
     "title": "Value", 
 
     "percentHeight": 70, 
 
     "stockGraphs": [{ 
 
     \t \t "fillAlphas": 0, 
 
\t \t \t \t \t \t "fillColors":"red", 
 
      "id": "g1", 
 
      "valueField": "value", 
 
      "type": "smoothedLine", 
 
      "lineThickness": 2, 
 
      "bullet": "round", 
 
      
 
     }], 
 
     "stockLegend": { 
 
      "valueTextRegular": " ", 
 
      "markerType": "none" 
 
     } 
 
    }, 
 
    { 
 
     "title": "Volume", 
 
     "percentHeight": 30, 
 
     "valueAxes": [{ 
 
      "id": "ValueAxis-1", 
 
      "title": "Axis title" 
 
     }], 
 
     "stockGraphs": [{ 
 
       "valueField": "volume", 
 
       "type": "column", 
 
       "cornerRadiusTop": 2, 
 
       "fillAlphas": 1, 
 
         "fillColorsField":"red" 
 
      }, 
 
      { 
 
       "valueField": "value", 
 
       //"type": "column", 
 
       "cornerRadiusTop": 5,"fillColorsField":"red" 
 
      } 
 
     ], 
 
     "stockLegend": { 
 
      "valueTextRegular": " ", 
 
      "markerType": "none" 
 
     } 
 
    }], 
 
    "chartScrollbarSettings": { 
 
     "graph": "g1", 
 
     "usePeriod": "10mm", 
 
     "position": "top" 
 
    }, 
 

 
    "chartCursorSettings": { 
 
     "valueBalloonsEnabled": true 
 
    }, 
 

 
    "periodSelector": { 
 
     "position": "top", 
 
     "dateFormat": "YYYY-MM-DD JJ:NN", 
 
     "inputFieldWidth": 150, 
 
     "periods": [{ 
 
      "period": "hh", 
 
      "count": 1, 
 
      "label": "1 hour", 
 
      "selected": true 
 
     }, { 
 
      "period": "hh", 
 
      "count": 2, 
 
      "label": "2 hours" 
 
     }, { 
 
      "period": "hh", 
 
      "count": 5, 
 
      "label": "5 hour" 
 
     }, { 
 
      "period": "hh", 
 
      "count": 12, 
 
      "label": "12 hours" 
 
     }, { 
 
      "period": "MAX", 
 
      "label": "MAX" 
 
     }] 
 
    }, 
 

 
    "panelsSettings": { 
 
     "usePrefixes": true 
 
    }, 
 

 
    "export": { 
 
     "enabled": true, 
 
     "position": "bottom-right" 
 
    } 
 
});
#chartdiv { 
 
    width: 100%; 
 
    height: 400px; 
 
} 
 
.amChartsPanel rect.amcharts-graph-column-front.amcharts-graph-column-element { 
 
    fill: #000; 
 
    stroke: #000; 
 
}
<script src="https://www.amcharts.com/lib/3/amcharts.js"></script> 
 
<script src="https://www.amcharts.com/lib/3/serial.js"></script> 
 
<script src="https://www.amcharts.com/lib/3/amstock.js"></script> 
 
<script src="https://www.amcharts.com/lib/3/plugins/export/export.min.js"></script> 
 
<link rel="stylesheet" href="https://www.amcharts.com/lib/3/plugins/export/export.css" type="text/css" media="all" /> 
 
<script src="https://www.amcharts.com/lib/3/themes/light.js"></script> 
 
<div id="chartdiv"></div> \t

+0

氣球邊框顏色應與圖形顏色相同。黑色應該有黑色的氣球邊框,這樣很容易識別。感謝您的回覆。 –

+0

好吧。 @xorspark的答案是正確的。我明白他的解答。謝謝 – Dhaarani