Skip to content
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

Doesn't work with 2.8.0 #156

Closed
Svetomechc opened this issue Mar 18, 2019 · 23 comments
Closed

Doesn't work with 2.8.0 #156

Svetomechc opened this issue Mar 18, 2019 · 23 comments

Comments

@Svetomechc
Copy link
Contributor

Is there any workaround to get the plugin working with chart.js 2.8.0?

@Svetomechc
Copy link
Contributor Author

So, it seems to me that 2.7.0 breaks lines and 2.8.0 breaks boxes. Any plans on fixing that?

@benmccann
Copy link
Collaborator

@Svetomechc there isn't anyone who is actively working on this plugin, but if you send a PR to fix it then I will review it

@nullpointer77
Copy link

@Svetomechc For what it's worth, i ended up abandoning this plugin as well as the draggable plugin as it's pretty simple to just create your own custom chartjs plugins per their documentation. If you really need draggable functionality D3 has a pretty solid implementation you can use.

@kurkle
Copy link
Member

kurkle commented Mar 19, 2019

I'm using it with 2.8.0, how does it break for you?

@Svetomechc
Copy link
Contributor Author

@benmccann not so sure of my JS skills (:

@nullpointer77 might try that

@kurkle I use it like so (type: 'box'):

    const annotations = this.selectedCip.cipSteps.map(v => {

      const color = new Color('#' + v.cipStepType.color);
      color.a = 0.3;
      return {
        drawTime: 'beforeDatasetsDraw',
        type: 'box',
        xScaleID: 'x1',
        yScaleID: 'dummy',
        xMin: v.start,
        xMax: v.end,
        yMin: 0,
        yMax: 1,
        backgroundColor: color.toRgba(),
        borderWidth: 0,
      };
    });
    this.lineChartOptions.annotation = { annotations: annotations };

@alexis-landrieu-avanade
Copy link

alexis-landrieu-avanade commented Apr 5, 2019

Hello @Svetomechc , could you share your imports statement ?

For angular by example we need to follow these steps :

import * as ChartAnnotation from 'chartjs-plugin-annotation';

ngOnInit() {
  let caPlugin = ChartAnnotation;
  caPlugin["id"]="annotation";
  Chart.pluginService.register(caPlugin);
}

This following imports statement seems to not be working anymore.

import 'chartjs-plugin-annotation';

@Svetomechc
Copy link
Contributor Author

@alexis-landrieu-avanade thank you!

@flaushi
Copy link

flaushi commented Apr 17, 2019

Hmm, for me the annotations do work with 2.8.0 - at least as long as I do not resize the canvas...

@ghost
Copy link

ghost commented Jun 5, 2019

Im also having issues with this even after following @alexis-landrieu-avanade suggestion

const myChart = new Chart(this.ctx, {
            type: 'line',
            plugins: [ChartAnnotation],
            data: {
                labels: stringLabels,
                datasets: [{
                    fill: true,
                    lineTension: 0.5,
                    pointRadius: 0, // Removes indiviual points/tooltips
                    borderWidth: 1,
                    label: 'Monthly Spending',
                    data: data,
                    backgroundColor: 'rgb(124,190,235,0.6)',
                    borderColor: 'rgb(63,81,181)'
                }]
            },
            options: {
                plugins: {
                    annotation: {
                        annotations: [
                            {
                                type: 'line',
                                mode: 'vertical',
                                scaleID: 'x-axis-0',
                                value: 2,
                                borderColor: 'red',
                                borderWidth: 2,
                            }
                        ]
                    }
                },
            }
        });

@Svetomechc
Copy link
Contributor Author

Svetomechc commented Jun 5, 2019

Still works for me with @alexis-landrieu-avanade fix:

image

But recently chartjs-plugin-zoom stopped working. Currently investigating why...
UPD: Was error on my end.

@ghost
Copy link

ghost commented Jun 5, 2019

    "chart.js": "^2.8.0",
    "chartjs-plugin-annotation": "^0.5.7",

This is my npm package, i think the version is compatible?

@ghost
Copy link

ghost commented Jun 5, 2019

I fixed it, i had 4 problems.

  1. You have to define the plugin
   import * as ChartAnnotation from 'chartjs-plugin-annotation';
   ...
   plugins: [ChartAnnotation],
  1. Where to place the annotation, the documentation makes this very confusing by showing it within options.plugins, when actually it only works as as a child of options.

  2. When the annotation value is of type int, it will not work if mode is vertical, but it will work if mode if horizontal. because my line was vertical i had to convert this value to a string.

  3. If you are using types its going to complain about annotations so you should add as ChartOptions

     const myChart = new Chart(this.ctx, {
            type: 'line',
            plugins: [ChartAnnotation],
            data: {
                labels: stringLabels,
                datasets: [{
                    fill: true,
                    lineTension: 0.5,
                    pointRadius: 0, // Removes indiviual points/tooltips
                    borderWidth: 1,
                    label: 'Monthly Spending',
                    data: data,
                    backgroundColor: 'rgb(124,190,235,0.6)',
                    borderColor: 'rgb(63,81,181)'
                }]
            },
            options: {
                annotation: {
                    annotations: [
                        {
                            type: 'line',
                            mode: 'vertical',
                            scaleID: 'x-axis-0',
                            value: payday.toString(),
                            borderColor: 'red',
                            borderWidth: 2,
                        }
                    ]
                }
            } as ChartOptions
        });

@benmccann
Copy link
Collaborator

The issue is that the README shows the setup for the code in master (#130), but the last time a new version was released was Oct 2017. We should release a new version of the plugin to fix it

@benmccann
Copy link
Collaborator

Although it looks like we might need a fix first: #130 (comment)

If anyone wants to send a fix, I can help get it reviewed and merged and a new version released

@jazzy1986
Copy link

I fixed it, i had 4 problems.

  1. You have to define the plugin
   import * as ChartAnnotation from 'chartjs-plugin-annotation';
   ...
   plugins: [ChartAnnotation],
  1. Where to place the annotation, the documentation makes this very confusing by showing it within options.plugins, when actually it only works as as a child of options.
  2. When the annotation value is of type int, it will not work if mode is vertical, but it will work if mode if horizontal. because my line was vertical i had to convert this value to a string.
  3. If you are using types its going to complain about annotations so you should add as ChartOptions
     const myChart = new Chart(this.ctx, {
            type: 'line',
            plugins: [ChartAnnotation],
            data: {
                labels: stringLabels,
                datasets: [{
                    fill: true,
                    lineTension: 0.5,
                    pointRadius: 0, // Removes indiviual points/tooltips
                    borderWidth: 1,
                    label: 'Monthly Spending',
                    data: data,
                    backgroundColor: 'rgb(124,190,235,0.6)',
                    borderColor: 'rgb(63,81,181)'
                }]
            },
            options: {
                annotation: {
                    annotations: [
                        {
                            type: 'line',
                            mode: 'vertical',
                            scaleID: 'x-axis-0',
                            value: payday.toString(),
                            borderColor: 'red',
                            borderWidth: 2,
                        }
                    ]
                }
            } as ChartOptions
        });

thanks, you save my life, take me whole morning to solve this, love you so much. it actually need to be inside OPTIONS not beside it. Chart.js document is suck

@iamjonmiller
Copy link

iamjonmiller commented Oct 15, 2019

Just trying to import this plugin spits out this error in the chrome console:

chartjs-plugin-annotation.min.js:10 Uncaught TypeError: Cannot read property 'Annotation' of undefined at Object.6../annotation.js (chartjs-plugin-annotation.min.js:10) at o (chartjs-plugin-annotation.min.js:10) at e (chartjs-plugin-annotation.min.js:10) at chartjs-plugin-annotation.min.js:10 6../annotation.js @ chartjs-plugin-annotation.min.js:10 o @ chartjs-plugin-annotation.min.js:10 e @ chartjs-plugin-annotation.min.js:10 (anonymous) @ chartjs-plugin-annotation.min.js:10

Also using with 2.8.0 and cdn here https://cdnjs.com/libraries/chartjs-plugin-annotation

@kurkle
Copy link
Member

kurkle commented Oct 16, 2019

Just trying to import this plugin spits out this error in the chrome console:

chartjs-plugin-annotation.min.js:10 Uncaught TypeError: Cannot read property 'Annotation' of undefined at Object.6../annotation.js (chartjs-plugin-annotation.min.js:10) at o (chartjs-plugin-annotation.min.js:10) at e (chartjs-plugin-annotation.min.js:10) at chartjs-plugin-annotation.min.js:10 6../annotation.js @ chartjs-plugin-annotation.min.js:10 o @ chartjs-plugin-annotation.min.js:10 e @ chartjs-plugin-annotation.min.js:10 (anonymous) @ chartjs-plugin-annotation.min.js:10

Also using with 2.8.0 and cdn here https://cdnjs.com/libraries/chartjs-plugin-annotation

Did you import Chart.js before the plugin?

@iamjonmiller
Copy link

Just trying to import this plugin spits out this error in the chrome console:
chartjs-plugin-annotation.min.js:10 Uncaught TypeError: Cannot read property 'Annotation' of undefined at Object.6../annotation.js (chartjs-plugin-annotation.min.js:10) at o (chartjs-plugin-annotation.min.js:10) at e (chartjs-plugin-annotation.min.js:10) at chartjs-plugin-annotation.min.js:10 6../annotation.js @ chartjs-plugin-annotation.min.js:10 o @ chartjs-plugin-annotation.min.js:10 e @ chartjs-plugin-annotation.min.js:10 (anonymous) @ chartjs-plugin-annotation.min.js:10
Also using with 2.8.0 and cdn here https://cdnjs.com/libraries/chartjs-plugin-annotation

Did you import Chart.js before the plugin?

Apparently not, I thought I had tried that. Combined with KayHS solution above (manually importing) I have annotations working on 2.8.0 now. Thanks!

@sukensheth
Copy link

Hi. I'm trying to import chartjs-annotation-plugin in my Angular project. Even after the npm install and
import * as ChartAnnotation from 'chartjs-plugin-annotation';

I am getting this :
Could not find a declaration file for module 'chartjs-plugin-annotation'. 'c:/XRay/xray/node_modules/chartjs-plugin-annotation/src/index.js' implicitly has an 'any' type.
Try npm install @types/chartjs-plugin-annotation if it exists or add a new declaration (.d.ts) file containing declare module 'chartjs-plugin-annotation';

@sfentress
Copy link

Thank you all for this helpful thread. I'll just add some details about the changes I had to make for those who are upgrading to 2.8.0 and using React, which hasn't been touched on above:

  1. Add ChartAnnotation to the plugins field of your chart component:
-import "chartjs-plugin-annotation";
+import * as ChartAnnotation from "chartjs-plugin-annotation";

...

<Scatter
  data={data}
  options={options}
+  plugins={[ChartAnnotation]}
/>
  1. (Already mentioned above, but adding here for clarity): Move the annotation plugin definition up a level, out from the plugins section of options . Shown here with TypeScript:
const defaultOptions: ChartOptions = {
  title: ...
- plugins: {
-   annotation: {
-     drawTime: "afterDraw",
-     events: ["click", "mouseenter", "mouseleave"],
-     annotations: [ ... ]
-   }
+ annotation: {
+   drawTime: "afterDraw",
+   events: ["click", "mouseenter", "mouseleave"],
+   annotations: [ ... ]
+ }
-};
+} as ChartOptions;
  1. For TypeScript, declare the module in an index.d.ts file (to answer @sukensheth 's question):
+declare module "chartjs-plugin-annotation";
  1. Maybe unrelated, but the label position "top" no longer worked after the upgrade. I had to hack it with label: { position: "bottom", yAdjust: 300}.

This was referenced Jan 13, 2020
@philippfrank
Copy link

@benmccann

If anyone wants to send a fix, I can help get it reviewed and merged and a new version released

Are you still a maintainer of this project? Could you release a new version?

@abhijiy
Copy link

abhijiy commented Dec 2, 2020

My functionality is describe below:
After click on line chart I need to show the annotation line.I have included annotation plugin. But it is not working.Annotation plugin is not called.

@etimberg
Copy link
Member

etimberg commented Mar 8, 2021

Closing this thread as it contained a bunch of different issues that appear to be resolved

  1. New versions (1.0.0-beta.1 to 1.0.0-beta.4) released to work with chart.js v3.0.0-beta.*
  2. Typescript types now ship with the plugin
  3. Documentation issues tracked elsewhere (e.g. Update the examples on the README #336 and Host documentation & samples #343)

@etimberg etimberg closed this as completed Mar 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests