score:2

Accepted answer

few things wrong with your code:

  • legend namespace was in v2 place instead of v3, in v3 it has been moved to options.plugins.legend
  • scales where defined as arrays which is v2 syntax, in v3 all scales are their own object where the scale id is the object key
  • backgroundcolorhover is not a valid property, you should use the hoverbackgroundcolor to define that color, also to make it the same color on hover you shouldnt define it as null but instead define it as the same color.
  • the beginatzero is not defined in the ticks but on the root of the scale now, but since it does nothing on a category scale i have removed it.

for all changes between v2 and v3 you can read the migration guide

since scale config now works to make it more clean you can remove all the unecesarry stacks in each dataset, also you dont have to specify that its a bar dataset since the default type takes care of that. you only need to specify it if you are deviating from it.

<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/3.5.1/chart.min.js"></script>
<!doctype html>
<html>

<head>
  <title>bar chart</title>

  <script src="chart.min.js"></script>
  <meta name="viewport" content="initial-scale = 1, user-scalable = no" />
  <style>
    canvas {}
  </style>
</head>

<body>
  <canvas id="canvas" height="450" width="600"></canvas>
  <script>
    var model = {
      "type": "bar",
      "data": {
        "labels": [
          "sob - base case",
          "sob simulated product"
        ],
        "datasets": [{
            "label": "1 drop only: 1 active ingredient only",
            "backgroundcolor": [
              "#a76fde"
            ],
            "data": [
              0.0364,
              0.0911
            ],
            "hoverbackgroundcolor": "#a76fde"
          },
          {
            "label": "1 fixed dose drop combination only",
            "backgroundcolor": [
              "#caa9eb"
            ],
            "data": [
              0.0189,
              0.0517
            ],
            "hoverbackgroundcolor": "#caa9eb"
          },
          {
            "label": "multiple drops only, excluding fixed dose combinations only",
            "backgroundcolor": [
              "#b4c12f"
            ],
            "data": [
              0.025,
              0.0487
            ],
            "hoverbackgroundcolor": "#b4c12f"
          },
          {
            "label": "selective laser trabeculoplasty (slt)",
            "backgroundcolor": [
              "#002060"
            ],
            "data": [
              0.0121,
              0.0293
            ],
            "hoverbackgroundcolor": "#002060"
          },
          {
            "label": "other laser treatments for glaucoma",
            "backgroundcolor": [
              "#009999"
            ],
            "data": [
              0.0025,
              0.0071
            ],
            "hoverbackgroundcolor": "#009999"
          },
          {
            "label": "migs",
            "backgroundcolor": [
              "#da1884"
            ],
            "data": [
              0.0004,
              0.0109
            ],
            "hoverbackgroundcolor": "#da1884"
          },
          {
            "label": "traditional incisional surgery",
            "backgroundcolor": [
              "#fdc741"
            ],
            "data": [
              0,
              0.0095
            ],
            "hoverbackgroundcolor": "#fdc741"
          }
        ]
      },
      "options": {
        "scales": {
          "x": {
            "stacked": true,
            "ticks": {
              "maxrotation": 0,
              "minrotation": 0
            }
          },
          "y": {
            "stacked": true
          }
        },
        "responsive": true,
        "plugins": {
          "legend": {
            "display": true,
            "position": "right",
            "align": "start"
          }
        }

      }
    }


    new chart(document.getelementbyid("canvas").getcontext("2d"), model);
  </script>
</body>

</html>

score:-1

you need to wrap legend inside of plugins see example (click the actions tab to see how they adjust the legend position) or this example where it is explicitly written in the config

<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/3.5.1/chart.min.js"></script>
<!doctype html>
<html>

<head>
    <title>bar chart</title>

    <script src="chart.min.js"></script>
    <meta name="viewport" content="initial-scale = 1, user-scalable = no" />
    <style>
        canvas {}
    </style>
</head>

<body>
    <canvas id="canvas" height="450" width="600"></canvas>
    <script>

        var model = {
            "type": "bar",
            "data": {
                "labels": [
                    "sob - base case",
                    "sob simulated product"
                ],
                "datasets": [
                    {
                        "label": "1 drop only: 1 active ingredient only",
                        "type": "bar",
                        "stack": null,
                        "backgroundcolor": [
                            "#a76fde"
                        ],
                        "data": [
                            0.0364,
                            0.0911
                        ],
                        "backgroundcolorhover": null
                    },
                    {
                        "label": "1 fixed dose drop combination only",
                        "type": "bar",
                        "stack": null,
                        "backgroundcolor": [
                            "#caa9eb"
                        ],
                        "data": [
                            0.0189,
                            0.0517
                        ],
                        "backgroundcolorhover": null
                    },
                    {
                        "label": "multiple drops only, excluding fixed dose combinations only",
                        "type": "bar",
                        "stack": null,
                        "backgroundcolor": [
                            "#b4c12f"
                        ],
                        "data": [
                            0.025,
                            0.0487
                        ],
                        "backgroundcolorhover": null
                    },
                    {
                        "label": "selective laser trabeculoplasty (slt)",
                        "type": "bar",
                        "stack": null,
                        "backgroundcolor": [
                            "#002060"
                        ],
                        "data": [
                            0.0121,
                            0.0293
                        ],
                        "backgroundcolorhover": null
                    },
                    {
                        "label": "other laser treatments for glaucoma",
                        "type": "bar",
                        "stack": null,
                        "backgroundcolor": [
                            "#009999"
                        ],
                        "data": [
                            0.0025,
                            0.0071
                        ],
                        "backgroundcolorhover": null
                    },
                    {
                        "label": "migs",
                        "type": "bar",
                        "stack": null,
                        "backgroundcolor": [
                            "#da1884"
                        ],
                        "data": [
                            0.0004,
                            0.0109
                        ],
                        "backgroundcolorhover": null
                    },
                    {
                        "label": "traditional incisional surgery",
                        "type": "bar",
                        "stack": null,
                        "backgroundcolor": [
                            "#fdc741"
                        ],
                        "data": [
                            0,
                            0.0095
                        ],
                        "backgroundcolorhover": null
                    }
                ]
            },
            "options": {
                "scales": {
                    "xaxes": [
                        {
                            "stacked": true,
                            "ticks": {
                                "beginatzero": true,
                                "maxrotation": 0,
                                "minrotation": 0
                            }
                        }
                    ],
                    "yaxes": [
                        {
                            "stacked": true
                        }
                    ]
                },
                "responsive": true,
                "plugins": {
                  "legend": {
                    "align": "start",
                    "position": "right"
                  }
                }
            }
        }


        new chart(document.getelementbyid("canvas").getcontext("2d"), model);

    </script>
</body>

</html>


Related Query

More Query from same tag