fir*_*ait 30 charts google-visualization
我正在使用javascript api设计谷歌图表.我想更改绘制数据的区域的背景.出于某种原因,当我设置背景选项时:
chart.draw(data, { backgroundColor: { fill: "#F4F4F4" } })
Run Code Online (Sandbox Code Playgroud)
它会更改整个图表的背景,而不会更改绘制数据的区域.关于如何仅改变绘图区域背景的任何想法?谢谢
小智 50
传递这样的选项
var options = {
title: 'title',
width: 310,
height: 260,
backgroundColor: '#E4E4E4',
is3D: true
};
Run Code Online (Sandbox Code Playgroud)
Kri*_*ote 12
将此添加到您的选项:
'chartArea': {
'backgroundColor': {
'fill': '#F4F4F4',
'opacity': 100
},
}
Run Code Online (Sandbox Code Playgroud)
The proper answer is that it depends if it is classic Google Charts or Material Google Charts. If you use classic version of the Google Charts, multiple of the above suggestion work. However if you use newer Material type Google charts then you have to specify the options differently, or convert them (see google.charts.Bar.convertOptions(options)
below). On top of that in case of material charts if you specify an opacity for the whole chart, the opacity (only) won't apply for the chart area. So you need to explicitly specify color with the opacity for the chart area as well even for the same color combination.
一般而言:Google Charts 的 Material 版本缺少 Classic 的一些功能(倾斜轴标签、趋势线、自定义列着色、组合图表等等),反之亦然:数字格式和双重(三重, quadruple, ...) 轴仅支持 Material 版本。
如果两个特性都支持,材料图表有时需要不同的选项格式。
<body>
<div id="classic_div"></div>
<div id="material_div"></div>
</body>
Run Code Online (Sandbox Code Playgroud)
JS:
google.charts.load('current', { 'packages': ['corechart', 'bar'] });
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Year', 'Sales', 'Expenses'],
['2004', 1000, 400],
['2005', 1170, 460],
['2006', 660, 1120],
['2007', 1030, 540],
['2009', 1120, 580],
['2010', 1200, 500],
['2011', 1250, 490],
]);
var options = {
width: 1000,
height: 600,
chart: {
title: 'Company Performance',
subtitle: 'Sales, Expenses, and Profit: 2014-2017'
},
// Accepts also 'rgb(255, 0, 0)' format but not rgba(255, 0, 0, 0.2),
// for that use fillOpacity versions
// Colors only the chart area, simple version
// chartArea: {
// backgroundColor: '#FF0000'
// },
// Colors only the chart area, with opacity
chartArea: {
backgroundColor: {
fill: '#FF0000',
fillOpacity: 0.1
},
},
// Colors the entire chart area, simple version
// backgroundColor: '#FF0000',
// Colors the entire chart area, with opacity
backgroundColor: {
fill: '#FF0000',
fillOpacity: 0.8
},
}
var classicChart = new google.visualization.BarChart(document.getElementById('classic_div'));
classicChart.draw(data, options);
var materialChart = new google.charts.Bar(document.getElementById('material_div'));
materialChart.draw(data, google.charts.Bar.convertOptions(options));
}
Run Code Online (Sandbox Code Playgroud)
小提琴演示:https : //jsfiddle.net/csabatoth/v3h9ycd4/2/
使用选项更容易。
drawChart() {
// Standard google charts functionality is available as GoogleCharts.api after load
const data = GoogleCharts.api.visualization.arrayToDataTable([
['Chart thing', 'Chart amount'],
['Na Meta', 50],
['Abaixo da Meta', 22],
['Acima da Meta', 10],
['Refugos', 15]
]);
let options = {
backgroundColor: {
gradient: {
// Start color for gradient.
color1: '#fbf6a7',
// Finish color for gradient.
color2: '#33b679',
// Where on the boundary to start and
// end the color1/color2 gradient,
// relative to the upper left corner
// of the boundary.
x1: '0%', y1: '0%',
x2: '100%', y2: '100%',
// If true, the boundary for x1,
// y1, x2, and y2 is the box. If
// false, it's the entire chart.
useObjectBoundingBoxUnits: true
},
},
};
const chart = new GoogleCharts.api.visualization.ColumnChart(this.$.chart1);
chart.draw(data, options);
}
Run Code Online (Sandbox Code Playgroud)
我使用的是聚合物,这就是我使用 this.$.cart1 的原因,但是您可以使用 selectedbyid,没问题。