简体   繁体   English

C3JS的定制工具提示

[英]Customized tooltip for C3JS

I have created a chart using C3.JS. 我已经使用C3.JS创建了一个图表。 It has bar graph with line graph like as shown below 它具有条形图和折线图,如下所示

在此处输入图片说明

The graph is working fine but I have a requirement that I need a line graph to be shown within the tooltip along with other data points. 该图工作正常,但我要求我需要在工具提示中显示折线图以及其他数据点。

The line graph is coming but the other data points are missing (x, data1, data2). 线图即将到来,但其他数据点丢失(x,data1,data2)。 In addition tooltip should come only on mouse click not mouse over also by default when the page loads the tooltip should appear for the last bar. 另外,在页面加载时,默认情况下,仅在单击鼠标时出现工具提示,在页面加载时默认情况下也不会将鼠标悬停在工具提示上。

Current my tooltip shows only with the line graph like as shown below without the other data points(x, data1, data2) 当前,我的工具提示仅显示如下所示的折线图,而没有其他数据点(x,data1,data2)

在此处输入图片说明

But I expect my tooltip to appear like as shown below 但我希望工具提示如下所示

在此处输入图片说明

My code is as given below 我的代码如下

Can anyone please help me on this 有人可以帮我吗

Working JSFiddle 工作中的JSFiddle

function generateGraph(tooltip, data1, data2) {
    // if the data is same as before don't regenrate the graph - this avoids flicker
   if (tooltip.data1 && 
       (tooltip.data1.name === data1.name) && (tooltip.data1.value === data1.value) && 
       (tooltip.data2.name === data2.name) && (tooltip.data2.value === data2.value))
       return;

    tooltip.data1 = data1;
    tooltip.data2 = data2;

    // remove the existing chart
    if (tooltip.chart) {
        tooltip.chart = tooltip.chart.destroy();
        tooltip.selectAll('*').remove();
    }

    // create new chart
    tooltip.chart = c3.generate({
        bindto: tooltip,
          padding: {
          right: 15
            },
        size: {
            width: 200,
            height: 200
        },
        data: {
        columns: [
                ['data1', 30, 200, 100, 400, 150, 250, 160],
                ['data2', 50, 20, 10, 40, 15, 25, 34]
            ]
        },
        tooltip: {
            show: false
          }
    });

    // creating a chart on an element sets its position attribute to relative
    // reset it to absolute (the tooltip was absolute originally) for proper positioning
    tooltip.style('position', 'absolute');
    tooltip.style('background-color', 'white');
}

var chart = c3.generate({
    data: {
        columns: [
            ['x', 1000, 200, 150, 300, 200],
            ['data1', 1000, 200, 150, 300, 200],
            ['data2', 400, 500, 250, 700, 300], ],

            axes: {
                             'data1': 'y2'
                            },
            type: 'bar',
            types: {
              'data1': 'line'
            }
    },
    tooltip: {
        contents: function (d, defaultTitleFormat, defaultValueFormat, color) {
            // this creates a chart inside the tooltips
            var content = generateGraph(this.tooltip, d[0], d[1])
            // we don't return anything - see .html function below
            }
    }
});

// MONKEY PATCHING (MAY break if library updates change the code that sets tooltip content)
// we override the html function for the tooltip to not do anything (since we've already created the tooltip content inside it)
chart.internal.tooltip.html = function () {
    // this needs to return the tooltip - it's used for positioning the tooltip
    return chart.internal.tooltip;
}

Live Demo: http://jsfiddle.net/blackmiaool/y7Lhej4m/ 现场演示: http : //jsfiddle.net/blackmiaool/y7Lhej4m/

Instead of overriding the .html function of tooltip, I use contents and css to customize the tooltip. 我没有覆盖工具提示的.html功能,而是使用contents和CSS来自定义工具提示。

js: js:

contents: function (d, defaultTitleFormat, defaultValueFormat, color) {
    generateGraph(this.tooltip, d[0], d[1]);
    var tip = this.tooltip[0][0].querySelector(".c3-tooltip");//find previous tooltip
    if (tip) {
        tip.parentElement.removeChild(tip);//remove it
    }
    return this.getTooltipContent.apply(this, arguments) + this.tooltip.html();//concat default tooltip and  customized one
}

css: CSS:

.c3-tooltip-container {
    max-height: none !important;
}

.c3-tooltip {
    box-shadow: none;
    width: 100%;
}

.c3-tooltip tr {
    border-left: none !important;
    border-right: none !important;
}

.c3-tooltip td.name {
    border-left: none !important;
}

.c3-tooltip tr:first-child {
    display: none;
}

.c3-tooltip-container .c3-chart-line {
    opacity: 1 !important;
}

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM