我设法在我的图表上显示“无数据可用消息”。但麻烦的是,即使没有数据,我也希望用数据名称显示图例。
如果值为null,则“无数据”消息将消失。如果没有数据,将显示消息,但图例将消失。
var chart = c3.generate({
data: {
bindto: "#chart",
x: 'x',
columns: [
['x', '2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'],
//Uncomment to see the legend ['data1', null, null, null, null, null, null]
['data1'] //Comment out to see the legend
],
empty: {
label: {
text: "No Data Available"
}
}
},
axis: {
x: {
type: 'timeseries',
tick: {
format: '%Y-%m-%d'
}
}
}
});
chart.load({});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.10/c3.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.10/c3.js"></script>
<div id='chart'></div>
答案 0 :(得分:6)
看起来c3没有你想要的行为。我建议你编写这个函数,在这个函数中,你可以检查你是否有正常创建图表的数据:
c3.generate(config).load({})
但是,如果您没有数据,则可以使用null
值填充数据,并将带有文本No data available
的元素附加到根图表节点。您可以设置此文本的样式,它看起来像空数据的默认c3消息:
function createC3ChartWithConfig(config) {
if (config.data.columns[1].length < 2) { // if data not exist
const lengthOfTicks = config.data.columns[0].length - 1;
for (var i = 0; i < lengthOfTicks; i++) {
config.data.columns[1].push(null)
}
var chart = c3.generate(config);
chart.load({})
var element = document.createElement('div');
element.setAttribute('class', 'message');
element.innerText = 'No data available';
chart.element.appendChild(element)
} else {
c3.generate(config).load({})
}
}
检查以下演示。首先是没有数据的配置:
{
data: {
bindto: "#chart",
x: 'x',
columns: [
['x', '2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'],
['data1'] // data is empty
],
},
axis: {
x: {
type: 'timeseries',
tick: {
format: '%Y-%m-%d'
}
}
}
}
var config = {
data: {
bindto: "#chart",
x: 'x',
columns: [
['x', '2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'],
//['data1', 6, 4, 5, 5, 5, 5]
['data1'] //Comment out to see the legend
],
},
axis: {
x: {
type: 'timeseries',
tick: {
format: '%Y-%m-%d'
}
}
}
}
function createC3ChartWithConfig(config) {
if (config.data.columns[1].length < 2) { // if data not exist
const lengthOfTicks = config.data.columns[0].length - 1;
for (var i = 0; i < lengthOfTicks; i++) {
config.data.columns[1].push(null)
}
var chart = c3.generate(config);
chart.load({})
var element = document.createElement('div');
element.setAttribute('class', 'message');
element.innerText = 'No data available';
chart.element.appendChild(element)
} else {
c3.generate(config).load({})
}
}
createC3ChartWithConfig(config);
.message {
position: absolute;
top: 120px;
width: 100%;
font-size: 2em;
font-family: sans-serif;
color: #808080;
text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.10/c3.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.10/c3.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id='chart'></div>
第二个用于配置数据:
{
data: {
bindto: "#chart",
x: 'x',
columns: [
['x', '2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'],
['data1', 6, 4, 5, 4, 5, 2]
],
},
axis: {
x: {
type: 'timeseries',
tick: {
format: '%Y-%m-%d'
}
}
}
}
var config = {
data: {
bindto: "#chart",
x: 'x',
columns: [
['x', '2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'],
['data1', 6, 4, 5, 4, 5, 2]
],
},
axis: {
x: {
type: 'timeseries',
tick: {
format: '%Y-%m-%d'
}
}
}
}
function createC3ChartWithConfig(config) {
if (config.data.columns[1].length < 2) { // if data not exist
const lengthOfTicks = config.data.columns[0].length - 1;
for (var i = 0; i < lengthOfTicks; i++) {
config.data.columns[1].push(null)
}
var chart = c3.generate(config);
chart.load({})
var element = document.createElement('div');
element.setAttribute('class', 'message');
element.innerText = 'No data available';
chart.element.appendChild(element)
} else {
c3.generate(config).load({})
}
}
createC3ChartWithConfig(config);
.message {
position: absolute;
top: 120px;
width: 100%;
font-size: 2em;
font-family: sans-serif;
color: #808080;
text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.10/c3.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.10/c3.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id='chart'></div>