New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement basic box drawing #1064
base: master
Are you sure you want to change the base?
Conversation
That is a great feature! Is it hard to add an option to have the static width and scale only vertically when the chart is zoomed? |
It would be nice if this also takes in an array of boxes. Also what about being able to update the boxes? Currently if I am not mistaken you'll have to remove and re-add it again? I suppose a unified api/template would be handy across all markers, boxes, etc... (Add, Remove, Bulk Add, Bulk Delete, Update)? |
This could be added without much effort. I'm not quite sure what the use-case is for it, though (TradingView itself doesn't allow this either, unless I'm misunderstanding your comment). |
Yes, I'm not against adding any of these abilities. This PR only adds a very basic box drawing API that is supposed to mimic the pre-existing line drawing API. I'm happy to expand upon this if the maintainers would like this apart of the main repo. Otherwise, the box API in its current state is enough for my own private project needs, so I'll just close this PR. |
For those looking to use this feature, I had to do a couple of extra things in order to integrate this into my own project via NPM:
I did both of these in my boxes-install branch, so for those looking to use this, they can simply execute the following:
|
@tpunt can you add createBoxes api in it? |
@leosj I'm happy to extend the API, but only if this feature gets added to this repo. I'm still waiting for the owners here to comment on this PR - right now, it looks as though they are not interested in adding new features to lightweight-charts. This is a shame because the charting library is excellent, but it really lacks some quite basic, but very useful abilities. |
screen-20220729-154326.3.mp4It seems that the box disappears completely when the height of the box exceeds the visible y-axis scale. X-axis scale works fine. Attached video to illustrate what I mean. Edit: nvm I mixed up the lowPrice and highPrice in the box settings. It is working fine |
This works perfectly and is the unique feature I was requiring from Lightweight Charts. Thanks a ton for your work !! |
This is great. Just what i was looking for! Is it possible for someone to provide a method of including this in HTML such as https://jsfiddle.net/adrianntf/6qea5ytv/ That would be great. Thanks |
@juniormcmillan I'm not sure I understand why you'd want the ability to write HTML code, and have that display boxes on the chart. You could write JSON code to generate both HTML code and boxes, though. |
UpdateI've made an update to this PR. I wanted the ability to draw boxes that were rotated, but it appears that canvas does not allow you to draw boxes in that way. You can draw a simple box and then rotate it, but that would not scale properly with the axes on the chart. So instead of making a box a canvas rectangle, I've made it a series of connecting lines (which should have no functional change). To take advantage of this new flexibility to draw arbitrary shapes, you can specify a list of corners (2 or more), rather than the old parameters This is fully backwards compatible with the original PR (for those already relying upon it). Examples: Diagonal box exampleconst box = {
corners: [
{ time: 1641392200, price: 2902 },
{ time: 1641324200, price: 2902 },
{ time: 1641412200, price: 2750 },
{ time: 1641425200, price: 2750 },
],
borderColor: '#00f',
borderWidth: 3,
borderStyle: LightweightCharts.LineStyle.Dashed,
fillColor: '#0ff',
fillOpacity: 0.3,
borderVisible: true,
axisLabelVisible: false,
title: 'My box',
};
const createdBox = mainSeries.createBox(box);
// mainSeries.removeBox(createdBox); Arbitrary linesCreating horizontal, vertical, and diagonal lines (of set lengths): const shapes = [{
corners: [
{ time: 1641392200, price: 2902 },
{ time: 1641412200, price: 2750 },
],
borderColor: '#fff',
borderWidth: 2,
borderStyle: LightweightCharts.LineStyle.Dashed,
fillColor: '#fff',
fillOpacity: 0.3,
borderVisible: true,
axisLabelVisible: false,
title: 'My box',
},{
corners: [
{ time: 1641272200, price: 2875 },
{ time: 1641512200, price: 2875 },
],
borderColor: '#fff',
borderWidth: 2,
borderStyle: LightweightCharts.LineStyle.Dashed,
fillColor: '#fff',
fillOpacity: 0.3,
borderVisible: true,
axisLabelVisible: false,
title: 'My box',
},{
corners: [
{ time: 1641812200, price: 2700 },
{ time: 1641812200, price: 2900 },
],
borderColor: '#fff',
borderWidth: 2,
borderStyle: LightweightCharts.LineStyle.Dashed,
fillColor: '#fff',
fillOpacity: 0.3,
borderVisible: true,
axisLabelVisible: false,
title: 'My box',
}];
for (shape of shapes) {
const createdShape = mainSeries.createBox(shape);
// mainSeries.removeBox(createdShape);
} I realise the Changes are apart of the |
I haven't tried your updated code yet but from looking at it, am I correct in assuming that you can have more than 4 corners ? |
@0x0tyy Yes, you can specify 2 or more corners. 2 corners just means a straight line (in any direction). |
i tried to use this patch to create a kernel density estimated TPO market profile. Unfortunately, even after decreasing the tick size (to 1 , instead of 0.01), performance still drops after 2-3 profiles on screen. screen-20221214-165408.2.mp4 |
@mrle0pold Good catch! When I moved things to line-based drawing, the default border was a thin black line. I've now made a commit (6aa61f8) to allow you to (once again) disable the border completely ( |
@0x0tyy I assume it is because of the number of points needing to be drawn in order to render such shapes. Would it be possible to render bars instead of curves (similar to other Volume Profile indicators on TradingView)? They would be much friendlier, since there will be far fewer points. |
Really cool opportunity! Please release it! |
I was getting compile errors when taking the latest version of Light-weight charts with this code. Have fixed them and raised a PR in the form - cheriansk#1 |
Thank you @cheriansk for the commit, I ended up adding tpunt's changes for the package.json #1064 (comment) so that I can install the library with npm using the following command (just in case if someone else needs it):
|
Because shapes are now drawn with lines, the default line fill is thin and black. We must therefore override this when no border is desired.
is there any plans of merging this and pushing to prod? and/or make it compatible with 4.0.1? thanks! |
We'll soon release a plugin mechanism on top of the library that will allow you to achieve that request without necessarily awaiting for our approval. |
@mattou78400 This PR is already compatible with 4.0.1 - I rebased it against the master branch a month ago or so. |
Hii , Thank you |
@erfinbadrian Hey Erfin. In the current branch, no, this is not possible. However, I had the exact same need, and so I implemented this in a boxes-offscreen branch. Note that in order to get things to work, you must pass in a Note that the boxes-offscreen branch does not include the two commits from boxes-install (4a74c7d, 8c3f8b0), so you may want to cherry-pick them for a smoother |
can you please share the built branch with us? because im getting so confused with the errors when i try to build it myself |
@LmfaoHax I'm not sure I understand what you mean. I mentioned in one of the earlier comments that I made a couple of modifications to the build in the |
@tpunt when i try to install it i get these:
|
@LmfaoHax This does not really look like an issue with this specific branch. Given that others have also not had problems here, I'm guessing this is specific to your dependencies and their required versions. You could try clearing the npm cache ( |
@tpunt first of all thanks for your help. i managed to install it from boxes-install but now i use your first code example on this PR i get this error:
i assume that it is related to
but i dont know what i should be replacing it with |
@LmfaoHax My example was from the import { createChart, LineStyle } from 'lightweight-charts';
// LineStyle.Dashed |
@tpunt so i used it like the debug html and everything works like a charm. Thanks alot! |
Is there a possibility to set a corner at an arbitrary time, not dependent on timeframe? |
@syncros-juozas I'm not entirely sure what you mean. The only case where timeframe is used is for points that are off of the chart time scale (as I noted in my previous reply). Otherwise, timeframe is irrelevant. Or do you mean having the ability to specify points that are not aligned to the centre of a candlestick? |
@tpunt yes, points that are not aligned to the center of the candle |
@syncros-juozas I don't believe this is possible, at least not without some heavy modifications to the underlying library. In a private branch, I have aligned some drawings to the left-side of a candle (rather than in the centre), but it is still relative to a candle's time. |
I just want to thank you so much for adding this features, was searching for a viable library for so long, using plotly, but it gets to slow after a certain amount of styling and data added, so this is just awesome. |
hi, any updates? =) |
I'm trying to keep my PR updated with the latest changes in the I'm still waiting on @romfrancois for an update on the plugin mechanism status. Once ready, I will try to recreate my features using the new plugin system, so that things are less burdensome to keep up-to-date with the latest changes. |
Ping @romfrancois. Pretty please? :) |
It is close to release. The plugins support (and examples) are already on the master branch.
|
Version 4.1 has been released which adds Plugin support. We believe that this requested feature should be implemented as a plugin instead. As a result, we suggest closing this PR since it is unlikely that we would merge this to core library. Since there has been a lot of interest on this PR (and feature) in general, I think that a Plugin which implements box drawing would be very popular. The example plugins include 'Rectangle Drawing Tool', this could be used as a potential starting point. |
Finally!!! 😍😍😍😍
|
🎉 congrats. |
It looks like this requirement stems from the need to outline price consolidation. |
I have a build that let's you create a box and other drawing tools. Check out here. The goal it to get it into a plugin. |
can anybody here merge this draw box and multi-pane chart in one |
Type of PR: Enhancement
PR checklist:
Overview of change:
I have implemented basic box/rectangle drawing. This feature has been requested a few times in the past (#706, #408, in online chatrooms, on discord, etc). I realise it has been discussed to expose the raw canvas also as a potential solution, but it seems there is some complexity around this (e.g. there are multiple canvases). It also feels as though basic features such as this (that complement the line drawing abilities) would be much better suited to be integrated into the library itself (which would also benefit from having the objects rescaled upon the rescaling of axes).
Things done:
Things remaining to do:
If this is desirable to have in the main project, then I am happy to spend some more time on finishing things up. If this is not desirable though, then I'll simply use my fork, and this PR can be closed.
Code example:
Example pictures
Is there anything you'd like reviewers to focus on?
Whether this feature is desirable to have in the core library.
PR Update:
See #1064 (comment) of this PR for more abilities.