How to write a code to have a Vertical line for trading session during 12:00 pm and 1:00 pm in Pine Editor? - session

Vertical Line in the chart
For a trading session during 12:00 to 13:00 on chart

You can use bgcolor function to set the vertical background color where hour is between 12 and 13. Example
//#version = 5
indicator(title="test",overlay = true)
bgcolor(hour>=12 and hour<13?color.new(color.red,70):na)

Related

how to zoom on brushing with dc.js

I am trying to create a simple bar chart where the X axis is a date
scale and the Y a total count of events that occurred on a date.
Additionally I’m trying to add the capability to brush and zoom into the
chart so that there would be an initial X axis for years, then months
and finally days, but I am having an awful time trying to do this.
eventData.forEach(function(d) {
d.event_stat_data_ontime = $(d.event_stat_data_ontime).text();
//get rid of time portion
var tempDate = new Date(d.event_entry_date);
d.event_entry_date = new Date(tempDate.toDateString());
});
eventData = [ {event_entry_date: "Mon Feb 05 2020 23:13:39 GMT-0500 (Eastern Standard Time)", event_type: "Type 01"},
{event_entry_date: "Tue Feb 04 2020 00:14:40 GMT-0500 (Eastern Standard Time", event_type: "Type 02"},
{event_entry_date: "Thu Feb 20 2020 21:01:46 GMT-0500 (Eastern Standard Time)", event_type: "Type 01"},
{event_entry_date: "Fri Feb 21 2020 21:08:20 GMT-0500 (Eastern Standard Time)", event_type: "Type 01"},
{event_entry_date: "Mon Feb 03 2020 11:12:16 GMT-0500 (Eastern Standard Time)", event_type: "Type 03"} ];
eventData.forEach(function(d) {
d.event_entry_date = new Date(d.event_entry_date);
});
var ndx = crossfilter(eventData);
var dateDim = ndx.dimension( function(d) { return d.event_entry_date; } );
var dateGroup = dateDim.group().reduceCount( function(d) { return d.event_entry_date; });
var minDate = d3.timeMonth.offset(dateDim.bottom(1)[0].event_entry_date, -1);
var maxDate = d3.timeMonth.offset(dateDim.top(1)[0].event_entry_date, 1);
timeChart
.width( $('#dc-time-chart').parent().innerWidth() )
.transitionDuration(500)
.dimension(dateDim)
.group(dateGroup)
.elasticY(true)
.x(d3.scaleTime().domain([minDate, maxDate]))
.renderHorizontalGridLines(true)
dc.js doesn't have any built-in feature to zoom on brushing.
I can think of a few reasons for this:
The brush in dc.js is used for selection
You either end up with the brush covering the whole chart (ugly), or you have to remove the selection brush after zooming
You have to have some other way to restore the zoom, because once you are zoomed in, there is no way to select outside the chart to zoom out
Whatever the reason, the range and focus charts are the built-in solution for this.
If you still want to hack it, you can respond to the filtered event and change the domain of the x scale:
timeChart.on('filtered', function() {
if(timeChart.filter()) {
timeChart.x().domain(timeChart.filter());
timeChart.filter(null);
timeChart.redraw();
}
})
This takes the filter, applies it to the domain, and the removes the filter. This is a solution for problem #2 above. It's kind of ugly, but not as ugly as leaving the whole chart brushed afterward. (Also once the whole chart is brushed, there's no way to brush within that.)
By itself, this will not work right because dc.js charts brush continuously, while the mouse is being dragged.
There's an old feature request to have an option to only brush on "brush end", that is, when the mouse is released. We can borrow some code from a pull request that implements this feature.
We'll add this method to change the selection without filtering:
timeChart._nonFilteredBrushing = function () {
var extent = timeChart.extendBrush();
timeChart.fadeDeselectedArea();
};
And then change the brush event handlers to only filter on brush end:
timeChart.brush().on('brush', timeChart._nonFilteredBrushing);
timeChart.brush().on('end.filter', timeChart._brushing);
Unfortunately, this step has to be done after the chart is rendered.
Finally, we'll need to add a button to reset the domain of the chart, a solution for problem #3 above:
<a href='javascript:timeChart.focus(null)'>reset</a>
Here is a demo fiddle.
As for problem #1, how do we deal with selection, since this is different from the behavior of other dc.js charts?
1. No selection or filtering in this chart. That's what I implemented here.
2. Filter to the X domain of this chart, but don't show the brush. Maybe not difficult, but might require hiding the brush instead of removing the filter.
side note: quantizing to days.
Here's the idiomatic way to bin by days, rather than creating strings and then parsing them.
Use d3.timeDay in your dimension key function:
var dateDim = ndx.dimension( function(d) { return d3.timeDay(d.event_entry_date); } );
Use d3.timeDays for your xUnits so that dc.js knows how many bars there are and how wide to make them:
.xUnits(d3.timeDays)

labels to the axis - conver int (second) to hours

How can I make axis labels in the HOURS: MINUTES format (10:00 for example), if the data on the axis are represented as integers in the range [0...N]?
For example:
range [0..43200]
axis labels: 00:00 04:00 08:00 12:00
seconf example:
range [28800..172800]
axis labels: 08:00 16:00 24:00 08:00 16:00 24:00
When I use code:
set xdata time
set format x "%H:%M"
it does not work correctly :(
I assume you want that the time does not wrap at 24:00? i.e. 32:00 40:00 48:00 instead of 08:00 16:00 24:00 ?
See help time_specifiers
Simple test code:
reset session
set xdata time
set format x "%tH:%tM"
set xrange [0:180000]
plot x
which gives:
This option worked!
set xtics format "%tH:%tM" time
Unfortunately, it gives for the range [0: 172800] the values 0:00 - 48:00, and I would like 0:00-23:00-0:00-23:00

D3 line chart plotting backwards despite y axis being incremental in nature

I have a mix of a line chart and bar chart in D3. Though the y-axis(dates) mapped to 'usageDate' is incremental in nature, the resulting line graph seems to plot in the reverse direction.
Image
LineChart
The Line chart plots 'usageTrendDetails' on the X and usageDate on Y. Attaching a link with the code.
Clickhere
The line chart is plotting from left to right, however as highlighted in the image, it suddenly begins plotting from right to left and then goes back in the right direction. Any idea why this happening?
Sort your data before making the line:
data = data.sort(function(a,b){return a.date.getTime() - b.date.getTime();})
working code here
The data is sorted, the issue was with the way dates were being formatted after September. The logic for pre-pending a zero for months 0(Jan) to 8(Sep) wasn't handling Oct, Nov and Dec correctly, resulting in the months after September plotting backwards.
OLD CODE :
if(month<= 8){
month = "0"+(month+1);
}
NEW CODE : Handling Oct - Dec
if(month<= 8){
month = "0"+(month+1);
}else{
month = month + 1;
}
Here's the graph after the issue is fixed.
Click Here

Plotting a line graph from a starting point other than the origin - s3.js

I'm trying to plot data on a line graph where the x-axis are the months January to December.
The issue is: I can't seem to commence the line graph from anything other than the x-axis origin - which would be January.
Question - How can I set a start point, so the data line is plotted from March to December. I could set the data to be 0 for Jan and Feb, but I don't want the line to appear at these months.
Via http://c3js.org/samples/simple_regions.html - I can see you can set start and end regions to specify different types of lines (in this case dashes). Is there a similar way to set a start and end so January and February are hidden.
thanks
You can just use a null data point.
Here's sample code:
var chart = c3.generate({
data: {
columns: [
['data', null, null, 30, 200, 100, 400, 150, 250]
]
}
});
Here's the working fiddle: http://jsfiddle.net/jrdsxvys/1/

d3.time.scale() not working date inputs on IE 10 and Firefox

I am trying to build a bubble chart using d3.js. I want my bubble color to be filled according to date. I used the following function to generate color based on the date given:
color = d3.time.scale().domain([new Date('2013-07-23'), new Date('2013-06-01')]).range(['#98E698', '#1E7B1E']);
And while creating the graph I am calling it
.style("fill", function(d) {return colour(new Date("2013-07-8")); });
The above code is working fine only for Chrome. In IE 10 and Firefox the color function is returning NaN instead of a colour code. Why?
return color(new Date("2013-07-08"))
with color spelled correctly and a 0 in front of the 8 works in IE10. Reading the new Date(string) documentation, it looks like a two digit month is expected. In chrome:
>new Date("2013-07-8")
Mon Jul 08 2013 00:00:00 GMT-0700 (Pacific Daylight Time)
in IE10:
>new Date("2013-07-8")
Invalid Date
>new Date("2013-07-08")
Sun Jul 7 17:00:00 PDT 2013
To fix this problem - clean up your dates by adding a zero when necessary or use [d3.time.format] to change strings into dates3:
>var format = d3.time.format("%Y-%m-%d");
>format.parse("2013-07-8")
Mon Jul 8 00:00:00 PDT 2013
(this works in IE10)

Resources