2016-10-20 106 views
1

我有一個問題。我相對較新的JavaScript,但我正在一個項目,人們希望有關於他們的改進圖表。我成功製作了2張圖表,而第三張圖表確實存在問題。當y代表隨機數時,數字由0.000yyyy組成,當您懸停圖表時,信息顯示爲0.我將分數位數放在選項中,但無法讓它們正確工作。Google Charts fractionDigits

下面是代碼:

google.charts.load('current', {packages: ['corechart', 'line']}); 
google.charts.setOnLoadCallback(drawBackgroundColor); 

function drawBackgroundColor(transparent) { 
    var data = new google.visualization.DataTable(); 
    data.addColumn('date', 'X'); 
    data.addColumn('number', 'Xaurum Gold Growth'); 


    data.addRows([ 
[new Date(2015 , 03 , 15),0.000125], 
[new Date(2015 , 04 , 09),0.000125202590875], 
[new Date(2015, 04, 12), 0.000126019393875], 

    ]); 
var options = { 
    hAxis: { 
     title: 'Time', 
     textStyle:{color: '#FFF'}, 
     titleTextStyle: { 
    color: '#fff' 
} 
    }, 
    vAxis: { 
     title: 'Value', 
     textStyle:{color: '#FFF'}, 
     titleTextStyle: { 
    color: '#fff' 
} 
    }, 
    legend: { 
    textStyle: {color: '#fff'} 
}, 
NumberFormat: { 
    fractionDigits:15, 
}, 
annotations: { 
    boxStyle: { 
    stroke: '#765e34', 
    strokeWidth: 10, 
    } 
}, 
    backgroundColor: "transparent", 
    colors: ['#876c3c'], 


    }; 

    var chart = new google.visualization.LineChart(document.getElementById('charta_div')); 
    chart.draw(data, options); 
} 

回答

1

格式化數字的提示,使用NumberFormat,數據是建立

// format data 
    var formatter = new google.visualization.NumberFormat({ 
    fractionDigits: 15 
    }); 
    formatter.format(data, 1); 

看到下面的工作片段後...

google.charts.load('current', { 
 
    callback: drawBackgroundColor, 
 
    packages: ['corechart'] 
 
}); 
 

 
function drawBackgroundColor(transparent) { 
 
    var data = new google.visualization.DataTable(); 
 
    data.addColumn('date', 'X'); 
 
    data.addColumn('number', 'Xaurum Gold Growth'); 
 
    data.addRows([ 
 
    [new Date(2015 , 03 , 15), 0.000125], 
 
    [new Date(2015 , 04 , 09), 0.000125202590875], 
 
    [new Date(2015, 04, 12), 0.000126019393875] 
 
    ]); 
 

 
    // format data 
 
    var formatter = new google.visualization.NumberFormat({ 
 
    fractionDigits: 15 
 
    }); 
 
    formatter.format(data, 1); 
 

 
    var options = { 
 
    hAxis: { 
 
     title: 'Time', 
 
     textStyle:{ 
 
     color: '#FFF' 
 
     }, 
 
     titleTextStyle: { 
 
     color: '#fff' 
 
     } 
 
    }, 
 
    vAxis: { 
 
     title: 'Value', 
 
     textStyle:{ 
 
     color: '#FFF' 
 
     }, 
 
     titleTextStyle: { 
 
     color: '#fff' 
 
     } 
 
    }, 
 
    legend: { 
 
     textStyle: { 
 
     color: '#fff' 
 
     } 
 
    }, 
 
    annotations: { 
 
     boxStyle: { 
 
     stroke: '#765e34', 
 
     strokeWidth: 10, 
 
     } 
 
    }, 
 
    backgroundColor: 'transparent', 
 
    colors: ['#876c3c'] 
 
    }; 
 

 
    var chart = new google.visualization.LineChart(document.getElementById('chart_div')); 
 
    chart.draw(data, options); 
 
}
<script src="https://www.gstatic.com/charts/loader.js"></script> 
 
<div id="chart_div"></div>

編輯

造型註釋之前,您必須包含批註列

使用數據視圖使用功能「字符串」來添加列系列列

看到下面的工作片段...

google.charts.load('current', { 
 
    callback: drawBackgroundColor, 
 
    packages: ['corechart'] 
 
}); 
 

 
function drawBackgroundColor(transparent) { 
 
    var data = new google.visualization.DataTable(); 
 
    data.addColumn('date', 'X'); 
 
    data.addColumn('number', 'Xaurum Gold Growth'); 
 
    data.addRows([ 
 
    [new Date(2015, 03, 15), 0.000125], 
 
    [new Date(2015, 04, 09), 0.000125202590875], 
 
    [new Date(2015, 04, 12), 0.000126019393875], 
 
    [new Date(2015, 05, 22), 0.000126211199625], 
 
    [new Date(2015, 06, 07), 0.000127017994375], 
 
    [new Date(2015, 06, 08), 0.000127487763], 
 
    [new Date(2015, 06, 09), 0.000128022515125], 
 
    [new Date(2015, 06, 10), 0.00012886722975], 
 
    [new Date(2015, 06, 11), 0.00012921927025], 
 
    ]); 
 

 
    // add annotation column 
 
    var view = new google.visualization.DataView(data); 
 
    view.setColumns([0, 1, { 
 
    calc: 'stringify', 
 
    sourceColumn: 1, 
 
    type: 'string', 
 
    role: 'annotation' 
 
    }]); 
 

 
    var formatter = new google.visualization.NumberFormat({ 
 
    fractionDigits: 15 
 
    }); 
 
    formatter.format(data, 1); 
 

 
    var options = { 
 
    hAxis: { 
 
     title: 'Time', 
 
     textStyle: { 
 
     color: '#FFF' 
 
     }, 
 
     titleTextStyle: { 
 
     color: '#fff' 
 
     } 
 
    }, 
 
    vAxis: { 
 
     title: 'Value', 
 
     textStyle: { 
 
     color: '#FFF' 
 
     }, 
 
     titleTextStyle: { 
 
     color: '#fff' 
 
     } 
 
    }, 
 
    legend: { 
 
     textStyle: { 
 
     color: '#fff' 
 
     } 
 
    }, 
 
    annotations: { 
 
     boxStyle: { 
 
     stroke: '#876c3c', 
 
     strokeWidth:3 
 
     }, 
 
     textStyle: { 
 
     color: '#876c3c' 
 
     } 
 
    }, 
 
    backgroundColor: "transparent", 
 
    colors: ['#876c3c'] 
 
    }; 
 

 
    var chart = new google.visualization.LineChart(document.getElementById('charta_div')); 
 
    // use data view to draw chart 
 
    chart.draw(view, options); 
 
}
<script src="https://www.gstatic.com/charts/loader.js"></script> 
 
<div id="charta_div"></div>

+0

是的,它爲我工作!感謝您的幫助! – OrangeBud

+0

是的,我正在盡我最大的風格註釋,但正如我所說我很新的Javascript,所以我不明白在哪裏把所有的東西,我需要改變背景顏色,字體顏色,邊框。提前謝謝你的幫助! – OrangeBud

+0

我認爲我沒有做正確的事情,我做了希望,因爲我應該做的,但我不能看到任何事情發生,這裏是jsfiddle的例子。 [link] https://jsfiddle.net/pprh2rot/1/ [/ link] – OrangeBud