Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
NVD3 D3.JS问题。工具提示中显示为未定义的值(即使已定义)_D3.js_Tooltip_Undefined_Nvd3.js - Fatal编程技术网

NVD3 D3.JS问题。工具提示中显示为未定义的值(即使已定义)

NVD3 D3.JS问题。工具提示中显示为未定义的值(即使已定义),d3.js,tooltip,undefined,nvd3.js,D3.js,Tooltip,Undefined,Nvd3.js,我正在尝试用NVD3创建一个“linePlusBar”图表。x轴值为一年中的月份: var monthList = ['Ene 2016','Feb 2016','Mar 2016','Apr 2016','May 2014','Jun 2016','Jul 2016','Aug 2016', 'Sep 2016', 'Oct 2016', 'Nov 2016', 'Dec 2016']; 由于某些原因,工具提示中只有条形图显示此值,例如月份:2016年8月 而折线图在工具提示中将此值显示为“

我正在尝试用NVD3创建一个“linePlusBar”图表。x轴值为一年中的月份:

var monthList = ['Ene 2016','Feb 2016','Mar 2016','Apr 2016','May 2014','Jun 2016','Jul 2016','Aug 2016', 'Sep 2016', 'Oct 2016', 'Nov 2016', 'Dec 2016'];
由于某些原因,工具提示中只有条形图显示此值,例如月份:2016年8月

而折线图在工具提示中将此值显示为“月份:未定义”

有什么问题吗

这是我的图表:。下面是代码

function buildGraph(){
    //this will hold of our main data consists of multiple chart data
    var data = [];

    //variables to hold monthly month
    var monthList = ['Ene 2016','Feb 2016','Mar 2016','Apr 2016','May 2014','Jun 2016','Jul 2016','Aug 2016', 'Sep 2016', 'Oct 2016', 'Nov 2016', 'Dec 2016'];
    var monthlyIncome = [0, 2757820, 3447270, 0, 0, 0, 0, 0, 0, 0, 0, 0];
    var companiesNumber = [0, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0];

    //Array to hold each individual coordinate x and y values
    var monthlyIncomeValues = [];
    var companiesNumberValues = [];

    //Looping the data and fetch into array
    for(var i = 0; i < monthList.length; i++){
        var xyIncome = {x: i, y: monthlyIncome[i]};
        monthlyIncomeValues.push(xyIncome);

        var xyCompanies = {x: i, y: companiesNumber[i]};
        companiesNumberValues.push(xyCompanies);

    }

    //bar chart
    var dataIncome = { key: "Monthly Income", values: monthlyIncomeValues, type: "bar", yAxis: 2, color: '#00913B' }

    //line chart
    var dataCompanies = { key: "Amount of companies", values: companiesNumberValues, type: "line", yAxis: 1, color: '#00C3D9' }

    //Insert the values array into data variable
    data.push(dataIncome);
    data.push(dataCompanies);

    //build the graph
    nv.addGraph(function () {
        //build as multichart graphs and set the margin right and left to 100px.
        var chart = nv.models.multiChart()
                    .margin({left: 100, right: 100})

        //customize the tool tip
        chart.tooltip.contentGenerator(function (key, x, y, e, graph) {
            return "<div class='tooltip'><span>Month:</span> " + monthList[key.index] + "</div>" + "<div class='tooltip'><span>Value:</span> " + key.series[0].value + "</div><div class='tooltip'><span>Legend:</span> <div style='background:" + key.series[0].color + ";display:inline-block;height:15px;width:15px;'>&#160;</div></div>";
        });

        //Overwrite the x axis label and replace it with the month name
        chart.xAxis.tickFormat(function (d) { return monthList[d] });

        //Chart Interpolate
        chart.interpolate("linear")


        //Dollar Sign
        chart.yAxis2.tickFormat(function(d) { return '$' + d3.format(',f')(d) });

        //get the chart svg object and fecth the data to build the chart
        d3.select('#chart svg')
            .datum(data)
            .transition().duration(500).call(chart);
        return chart;
    });
}

//call the function to build the graph.
buildGraph();
函数构建图(){
//这将保持我们的主要数据由多个图表数据组成
var数据=[];
//每月要保存的变量
风险值月清单=['2016年2月'、'2016年3月'、'2016年4月'、'2014年5月'、'2016年6月'、'2016年7月'、'2016年8月'、'2016年9月'、'2016年10月'、'2016年11月'、'2016年12月'];
var Monthlyncome=[0,2757820,3447270,0,0,0,0,0,0,0,0];
var CompanyNumber=[0,4,5,0,0,0,0,0,0,0,0,0];
//数组以保存每个单独的坐标x和y值
var monthlyncomevalues=[];
var CompanyNumberValue=[];
//循环数据并提取到数组中
对于(变量i=0;i
原因是折线图的
不包括
索引
——而是有
点索引

试试这个:

monthList[key.index || key.pointIndex] 
演示: