I am trying to plot stock price data for a ticker using lightweight-charts.I can use it in expected way to draw chart for intervals like 1w, 1 month or 3 month etc. But Chart is not drawn as expected for one-day data.
Here are my part of code :
useEffect(() => {
const chart = createChart(ref.current, {
lineVisible: false,
width: 400,
height: 200,
layout: {
background: { color: "#26273000" },
textColor: "rgba(255, 255, 255, 0.9)",
},
leftPriceScale: {
borderVisible: false,
autoScale: true,
},
rightPriceScale: {
borderVisible: false,
autoScale: true,
},
grid: {
horzLines: {
visible: false,
},
vertLines: {
visible: false,
},
},
crosshair: {
mode: CrosshairMode.Magnet,
},
timeScale: {
minBarSpacing: 0.001,
borderVisible: false,
fixLeftEdge: true,
fixRightEdge: true,
},
priceScale: {
fixLeftEdge: true,
fixRightEdge: true,
minBarSpacing: 0.001,
},
localization: {
priceFormatter: (price) => {
return parseInt(price);
},
},
});
const areaSeries = chart.addAreaSeries();
const data = [
{ time: "2022-03-21T09:15:00", value: 502 },
{ time: "2022-03-21T09:30:00", value: 498.1 },
{ time: "2022-03-21T09:45:00", value: 495.6 },
{ time: "2022-03-21T10:00:00", value: 495.35 },
{ time: "2022-03-21T10:15:00", value: 495.6 },
{ time: "2022-03-21T10:30:00", value: 495.7 },
{ time: "2022-03-21T10:45:00", value: 495.5 },
{ time: "2022-03-21T11:00:00", value: 495.7 },
{ time: "2022-03-21T11:15:00", value: 495.5 },
{ time: "2022-03-21T11:30:00", value: 495.65 },
{ time: "2022-03-21T11:45:00", value: 494.1 },
{ time: "2022-03-21T12:00:00", value: 492.45 },
{ time: "2022-03-21T12:15:00", value: 492.7 },
{ time: "2022-03-21T12:30:00", value: 492.3 },
{ time: "2022-03-21T12:45:00", value: 490 },
{ time: "2022-03-21T13:00:00", value: 490.05 },
{ time: "2022-03-21T13:15:00", value: 489.95 },
{ time: "2022-03-21T13:30:00", value: 489.65 },
{ time: "2022-03-21T13:45:00", value: 491.05 },
{ time: "2022-03-21T14:00:00", value: 491.4 },
{ time: "2022-03-21T14:15:00", value: 492.8 },
{ time: "2022-03-21T14:30:00", value: 492.55 },
{ time: "2022-03-21T14:45:00", value: 490.8 },
{ time: "2022-03-21T15:00:00", value: 490.7 },
{ time: "2022-03-21T15:15:00", value: 491.05 },
{ time: "2022-03-21T15:30:00", value: 490.1 },
];
const visibleTimeRange = {};
visibleTimeRange.from = new Date(data[0].time).getTime() - 75000 * 6;
//7.5 minutes offset
visibleTimeRange.to = new Date(data[data.length - 1].time).getTime() + 75000 * 6;
//7.5 minutes offset
chart.timeScale().setVisibleLogicalRange(visibleTimeRange);
}, []);
return <div ref={ref}></div>;
However, the chart does not show time on its timeScale. The chart looks like a straight line drawn on the same day.
Chart for other range looks as expected:
Please help me in fixing this issue. Thanks!
Update-1: After I changed time to their microseconds format using new Date(date).getTime(),then i was able to see plot but time and date on x-axis were not correct.
This question has already been answered here.
Basically you need to properly format your timestamp as explained in the doc.
I want to draw Gantt chart like below
There is no option to draw Gantt chart in chart js. is it possible?? if not possible please suggest me some charting libraries to draw graph like this
I suggest you Scatter Chart. In Scatter Charts, you can draw multiple independent lines. As you can see from the below image.
[Sample Code]
var scatterChart = new Chart(ctx1, {
type: 'line',
data: {
datasets: [
{
label: 'Scatter Dataset',
backgroundColor: "rgba(246,156,85,1)",
borderColor: "rgba(246,156,85,1)",
fill: false,
borderWidth : 15,
pointRadius : 0,
data: [
{
x: 0,
y: 9
}, {
x: 3,
y: 9
}
]
},
{
backgroundColor: "rgba(208,255,154,1)",
borderColor: "rgba(208,255,154,1)",
fill: false,
borderWidth : 15,
pointRadius : 0,
data: [
{
x: 3,
y: 7
}, {
x: 5,
y: 7
}
]
},
{
label: 'Scatter Dataset',
backgroundColor: "rgba(246,156,85,1)",
borderColor: "rgba(246,156,85,1)",
fill: false,
borderWidth : 15,
pointRadius : 0,
data: [
{
x: 5,
y: 5
}, {
x: 10,
y: 5
}
]
},
{
backgroundColor: "rgba(208,255,154,1)",
borderColor: "rgba(208,255,154,1)",
fill: false,
borderWidth : 15,
pointRadius : 0,
data: [
{
x: 10,
y: 3
}, {
x: 13,
y: 3
}
]
}
]
},
options: {
legend : {
display : false
},
scales: {
xAxes: [{
type: 'linear',
position: 'bottom',
ticks : {
beginAtzero :true,
stepSize : 1
}
}],
yAxes : [{
scaleLabel : {
display : false
},
ticks : {
beginAtZero :true,
max : 10
}
}]
}
}
});
Rest the configuration like colors or if you want to hide the y axes do it as your project required.
EDIT this method would not work efficiently for more complicated cases where multiple bars need to be shown for a single Y value.
I would go with a stacked horizontalbar chart of two datasets. The first dataset would be transparent and used to offset the second dataset which is your actual data. The code below prevents tooltip from appearing for the first dataset as well.
http://codepen.io/pursianKatze/pen/OmbWvZ?editors=1111
[SAMPLE CODE]
var barOptions_stacked = {
hover :{
animationDuration:10
},
scales: {
xAxes: [{
label:"Duration",
ticks: {
beginAtZero:true,
fontFamily: "'Open Sans Bold', sans-serif",
fontSize:11
},
scaleLabel:{
display:false
},
gridLines: {
},
stacked: true
}],
yAxes: [{
gridLines: {
display:false,
color: "#fff",
zeroLineColor: "#fff",
zeroLineWidth: 0
},
ticks: {
fontFamily: "'Open Sans Bold', sans-serif",
fontSize:11
},
stacked: true
}]
},
legend:{
display:false
},
};
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'horizontalBar',
data: {
labels: ["1", "2", "3", "4"],
datasets: [{
data: [50,150, 300, 400, 500],
backgroundColor: "rgba(63,103,126,0)",
hoverBackgroundColor: "rgba(50,90,100,0)"
},{
data: [100, 100, 200, 200, 100],
backgroundColor: ['red', 'green', 'blue', 'yellow'],
}]
},
options: barOptions_stacked,
});
// this part to make the tooltip only active on your real dataset
var originalGetElementAtEvent = myChart.getElementAtEvent;
myChart.getElementAtEvent = function (e) {
return originalGetElementAtEvent.apply(this, arguments).filter(function (e) {
return e._datasetIndex === 1;
});
}
.graph_container{
display:block;
width:600px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.3/Chart.js"></script>
<html>
<body>
<div class="graph_container">
<canvas id="myChart"></canvas>
</div>
</body>
</html>
Another open source option is Frappé Gantt
You can try this library jQuery.Gantt. It is very useful and provide lots of options to draw Gantt Chart
An easy solution to this is to use quickchart.io
The good support people at quickchart.io were kind enough to send me an example that includes dates on the x-axis unlike some of the answers above. You can access the example here.
If you would then like to embed a Gantt chart into Gmail email you would first need to send the HTML to a service such as htmlcsstoimage.com
I think it's easier with Highcharts.
Check out their documentation.
It's really easy to use when it comes to project management charts.
Here is a JSFiddle link to example usage.
var today = new Date(),
day = 1000 * 60 * 60 * 24,
// Utility functions
dateFormat = Highcharts.dateFormat,
defined = Highcharts.defined,
isObject = Highcharts.isObject;
// Set to 00:00:00:000 today
today.setUTCHours(0);
today.setUTCMinutes(0);
today.setUTCSeconds(0);
today.setUTCMilliseconds(0);
today = today.getTime();
Highcharts.ganttChart('container', {
series: [{
name: 'Offices',
data: [{
name: 'New offices',
id: 'new_offices',
owner: 'Peter'
}, {
name: 'Prepare office building',
id: 'prepare_building',
parent: 'new_offices',
start: today - (2 * day),
end: today + (6 * day),
completed: {
amount: 0.2
},
owner: 'Linda'
}, {
name: 'Inspect building',
id: 'inspect_building',
dependency: 'prepare_building',
parent: 'new_offices',
start: today + 6 * day,
end: today + 8 * day,
owner: 'Ivy'
}, {
name: 'Passed inspection',
id: 'passed_inspection',
dependency: 'inspect_building',
parent: 'new_offices',
start: today + 9.5 * day,
milestone: true,
owner: 'Peter'
}, {
name: 'Relocate',
id: 'relocate',
dependency: 'passed_inspection',
parent: 'new_offices',
owner: 'Josh'
}, {
name: 'Relocate staff',
id: 'relocate_staff',
parent: 'relocate',
start: today + 10 * day,
end: today + 11 * day,
owner: 'Mark'
}, {
name: 'Relocate test facility',
dependency: 'relocate_staff',
parent: 'relocate',
start: today + 11 * day,
end: today + 13 * day,
owner: 'Anne'
}, {
name: 'Relocate cantina',
dependency: 'relocate_staff',
parent: 'relocate',
start: today + 11 * day,
end: today + 14 * day
}]
}, {
name: 'Product',
data: [{
name: 'New product launch',
id: 'new_product',
owner: 'Peter'
}, {
name: 'Development',
id: 'development',
parent: 'new_product',
start: today - day,
end: today + (11 * day),
completed: {
amount: 0.6,
fill: '#e80'
},
owner: 'Susan'
}, {
name: 'Beta',
id: 'beta',
dependency: 'development',
parent: 'new_product',
start: today + 12.5 * day,
milestone: true,
owner: 'Peter'
}, {
name: 'Final development',
id: 'finalize',
dependency: 'beta',
parent: 'new_product',
start: today + 13 * day,
end: today + 17 * day
}, {
name: 'Launch',
dependency: 'finalize',
parent: 'new_product',
start: today + 17.5 * day,
milestone: true,
owner: 'Peter'
}]
}],
tooltip: {
pointFormatter: function () {
var point = this,
format = '%e. %b',
options = point.options,
completed = options.completed,
amount = isObject(completed) ? completed.amount : completed,
status = ((amount || 0) * 100) + '%',
lines;
lines = [{
value: point.name,
style: 'font-weight: bold;'
}, {
title: 'Start',
value: dateFormat(format, point.start)
}, {
visible: !options.milestone,
title: 'End',
value: dateFormat(format, point.end)
}, {
title: 'Completed',
value: status
}, {
title: 'Owner',
value: options.owner || 'unassigned'
}];
return lines.reduce(function (str, line) {
var s = '',
style = (
defined(line.style) ? line.style : 'font-size: 0.8em;'
);
if (line.visible !== false) {
s = (
'<span style="' + style + '">' +
(defined(line.title) ? line.title + ': ' : '') +
(defined(line.value) ? line.value : '') +
'</span><br/>'
);
}
return str + s;
}, '');
}
},
title: {
text: 'Gantt Project Management'
},
xAxis: {
currentDateIndicator: true,
min: today - 3 * day,
max: today + 18 * day
}
});
#container {
max-width: 800px;
margin: 1em auto;
}
<script src="https://code.highcharts.com/gantt/highcharts-gantt.js"></script>
<script src="https://code.highcharts.com/gantt/modules/exporting.js"></script>
<div id="container"></div>
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Will setInterval cause browsers to hang?
i implement Highstock JS v1.1.4 chart for represent data
chart code :
chart = new Highcharts.StockChart({
chart: {
renderTo: 'highchartviewpanel',
events:{
load:function(){
setIntervalForhighchartdata();
}
}
},
rangeSelector: {
buttons: [{
count: 1,
type: 'minute',
text: '1M'
},{
count: 5,
type: 'minute',
text: '5M'
},
{
count: 15,
type: 'minute',
text: '15M'
}],
selected:0,
inputEnabled: false
},
navigator: {
height: 40,
xAxis: {
valueDecimals: 1
}
},
title: {
text: 'payment analysis',
floating: true,
align: 'right',
x: -20,
top: 20
},
xAxis: {
type: 'datetime',
dateTimeLabelFormats: {
second : '%H:%M:%S',
minute : '%H:%M',
hour: '%H',
day : '%b,%d',
week : 'b,%d',
month : '%Y,%b',
year : '%Y'
},
valueDecimals: 0
},
yAxis: [
{
title: {
text: 'item1'
},
height: 50,
lineWidth: 2
}, {
title: {
text: 'item2'
},
top: 90,
height: 50,
offset: 0,
lineWidth: 2
},{
title: {
text: 'item3'
},
top: 150,
height: 50,
offset: 0,
lineWidth: 2
}],
series: [{
name: 'item1',
data: item1data,
color:'blue'
},
{
name: 'item2',
yAxis: 1,
data: item2data,
color:'black'
},{
name: 'item3',
yAxis: 2,
data: item3data,
color:'red'
}]
});
}
setIntervalForhighchartdata() is function which update graph every 10 sec and my data value is base on time interval xdata is datetime and ydata is data point of 1,2,3 ....
its hang browser some time when graph update
The function your calling probably doesn't complete itself before you call it again.I assume you're using setInterval(). If so, I'd suggest using it combined with setTimeout().
For more info check out this thread - Will setInterval cause browsers to hang?