-
Notifications
You must be signed in to change notification settings - Fork 373
Add Label Badge to Line Chart. #12
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
Changes from all commits
07827d8
fa1fc6e
f395679
3965bfb
1065dda
dee0034
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,51 +1,71 @@ | ||
| // Line Annotation implementation | ||
| module.exports = function(Chart) { | ||
| var horizontalKeyword = 'horizontal'; | ||
| var verticalKeyword = 'vertical'; | ||
|
|
||
| var LineAnnotation = Chart.Element.extend({ | ||
|
|
||
| draw: function(ctx) { | ||
| var view = this._view; | ||
|
|
||
| // Canvas setup | ||
| ctx.lineWidth = view.borderWidth; | ||
| ctx.strokeStyle = view.borderColor; | ||
|
|
||
| // Draw | ||
| ctx.beginPath(); | ||
| ctx.moveTo(view.x1, view.y1); | ||
| ctx.lineTo(view.x2, view.y2); | ||
| ctx.stroke(); | ||
| } | ||
| }); | ||
|
|
||
| function lineUpdate(obj, options, chartInstance) { | ||
| var model = obj._model = obj._model || {}; | ||
|
|
||
| var scale = chartInstance.scales[options.scaleID]; | ||
| var pixel = scale ? scale.getPixelForValue(options.value) : NaN; | ||
| var chartArea = chartInstance.chartArea; | ||
|
|
||
| if (!isNaN(pixel)) { | ||
| if (options.mode == horizontalKeyword) { | ||
| model.x1 = chartArea.left; | ||
| model.x2 = chartArea.right; | ||
| model.y1 = model.y2 = pixel; | ||
| } else { | ||
| model.y1 = chartArea.top; | ||
| model.y2 = chartArea.bottom; | ||
| model.x1 = model.x2 = pixel; | ||
| } | ||
| } | ||
|
|
||
| model.borderColor = options.borderColor; | ||
| model.borderWidth = options.borderWidth; | ||
| } | ||
|
|
||
|
|
||
| return { | ||
| Constructor: LineAnnotation, | ||
| update: lineUpdate | ||
| }; | ||
| }; | ||
| module.exports = function (Chart) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. did the whitespace change here?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @etimberg It looks like it must have... not sure why github sees it as one big change. But that line was also readded below. |
||
| var horizontalKeyword = 'horizontal'; | ||
| var verticalKeyword = 'vertical'; | ||
|
|
||
| var LineAnnotation = Chart.Element.extend({ | ||
|
|
||
| draw: function (chartInstance, options) { | ||
| var view = this._view; | ||
| var ctx = chartInstance.chart.ctx; | ||
| var scale = chartInstance.scales[options.scaleID]; | ||
| var pixel = scale ? scale.getPixelForValue(options.value) : NaN; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need to call this again? would we be able to use the |
||
| var chartArea = chartInstance.chartArea; | ||
| // Canvas setup | ||
| ctx.lineWidth = view.borderWidth; | ||
| ctx.strokeStyle = view.borderColor; | ||
|
|
||
| // Draw | ||
| ctx.beginPath(); | ||
| ctx.moveTo(view.x1, view.y1); | ||
| ctx.lineTo(view.x2, view.y2); | ||
| ctx.stroke(); | ||
| //Verify that label has been specified, so that we don't add a null label. | ||
| //TODO: Added customizable badge options, like: where to display the badge, left/right/center. or, Badge Border Color and Badge Background color. | ||
| //I would also like to add an option for different directional offsets, horizontalOffset, and VertOffset for example. | ||
| //Current issue with background color is that it likes to disappear when you hover over any other element on the chart. | ||
| if (options.label) { | ||
| //draw the background of the badge. | ||
| ctx.fillStyle = 'white'; | ||
| ctx.fillRect((chartArea.right / 2), pixel - 5, 35, 15); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry that this took a while. I went to merge this and do some testing and I noticed this is incorrect. The width of the fill rect should be the width of the text. The text can be measured using The height of the rect would be the font size in px. |
||
| //Draw the border around the badge. | ||
| ctx.fillStyle = view.borderColor; | ||
| ctx.strokeRect((chartArea.right / 2), pixel - 5, 35, 15); | ||
| //Draw the text of the badge. | ||
| ctx.fillStyle = 'black'; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This colour should probably be configurable as well |
||
| ctx.textAlign = 'left'; | ||
| ctx.fillText(options.label ? options.label : '', (chartArea.right / 2) + 5, pixel - 5); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the extra check for label being present is not necessary. How will this location work for vertical lines? There should probably be a check for that case and change the position |
||
| } | ||
| } | ||
| }); | ||
|
|
||
| function lineUpdate(obj, options, chartInstance) { | ||
| var model = obj._model = obj._model || {}; | ||
|
|
||
| var scale = chartInstance.scales[options.scaleID]; | ||
| var pixel = scale ? scale.getPixelForValue(options.value) : NaN; | ||
| var chartArea = chartInstance.chartArea; | ||
|
|
||
| if (!isNaN(pixel)) { | ||
| if (options.mode == horizontalKeyword) { | ||
| model.x1 = chartArea.left; | ||
| model.x2 = chartArea.right; | ||
| model.y1 = model.y2 = pixel; | ||
|
|
||
| } else { | ||
| model.y1 = chartArea.top; | ||
| model.y2 = chartArea.bottom; | ||
| model.x1 = model.x2 = pixel; | ||
| } | ||
| } | ||
| model.borderColor = options.borderColor; | ||
| model.borderWidth = options.borderWidth; | ||
|
|
||
| } | ||
|
|
||
|
|
||
| return { | ||
| Constructor: LineAnnotation, | ||
| update: lineUpdate | ||
| }; | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably should be
beforeDatasetsDrawper my comment in the other issue