Spinning tags on the bottom portion of the circular graph

I'm currently utilizing Chart.js to create a pie chart.

My goal is to rotate the labels on the bottom half of the pie chart.

Below is my existing code for generating the pie chart:

$(document).ready(
  function() {
    var data = {
      datasets: [{
        data: [125, 125, 125, 125, 125, 125, 125, 125],
        backgroundColor: ["#FE4D42","#FFD405","#58E152","#00D59F","#8B7DFF","#E85FED","#F71663","#4F94FF"]
      }],
      labels: [
            "Arts and Creativity",
            "Careers and Employment",
            "Communities and Social",
            "Culture and Heritage",
            "Learning and Skills",
            "Personal Finance",
            "Self-Environment",
            "Wellbeing"
      ]
    };
      
    var labels_url = {
      "Arts and Creativity" : "arts-and-creativity",
      "Careers and Employment" : "careers-and-employment",
      "Communities and Social" : "communities-and-social",
      "Culture and Heritage" : "culture-and-heritage",
      "Wellbeing" : "health",
      "Learning and Skills" : "learning-and-skills",
      "Personal Finance" : "personal-finance",
      "Self-Environment" : "self-environment"
    };
    
    var canvas = document.getElementById("myChart");
    var ctx = canvas.getContext("2d");
    var myNewChart = new Chart(ctx,{
      type: 'pie',
      data: data,
      options: {
        legend: {
            display: false, 
        },
        plugins: {
          labels: {
            render: 'label',
            arc: true,
            fontColor:["#FF7805","#0854A3","#14A400","#048A7D","#502788","#F200F2","#FF0068","#000962"],
            fontSize: 18,
            position: 'outside'
          }
        }
      }
    });

    canvas.onclick = function(evt) {
      var activePoints = myNewChart.getElementsAtEvent(evt);
      if (activePoints[0]) {
        var chartData = activePoints[0]['_chart'].config.data;
        var idx = activePoints[0]['_index'];

        var label = chartData.labels[idx];
        var value = chartData.datasets[0].data[idx];

        var url = "https://www.maximiseme.co.uk/" + labels_url[label];
        //alert(url);
        console.log(url);
        window.location.href = url;
      }
    };
  }
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script>
<script src="https://cdn.jsdelivr.net/gh/emn178/chartjs-plugin-labels/src/chartjs-plugin-labels.js"></script>

<canvas id="myChart"></canvas>

To enhance readability, I would like to rotate the following labels by 180 degrees: Communities and Social, Culture and Heritage, Learning and Skills, Personal Finance.

Answer №1

Attempting to achieve a specific goal using the current libraries may prove challenging as the labels in question are governed by the chartjs-plugin-labels, which lacks the functionality to alter the orientation of the text rendering. Moreover, this plugin has not seen active development for the past 6 years.

However, in light of the succinct and clear sample provided, one potential solution involves implementing a workaround by manipulating the existing code within the chartjs-plugin-labels. Alternatively, one could explore the option of creating a fork of the plugin repository and incorporating similar modifications more directly.

The proposed workaround introduces a new configurable option under plugins.labels named reverse. When absent, the default value for reverse is false across all elements. The behavior mirrors that of fontColor, being both indexable and scriptable. If an arc element is assigned with the reverse: true attribute, its corresponding label will be displayed in a counter-clockwise fashion; otherwise, it follows the typical clockwise rotation.

No extensive elucidation is required, as the specifics can be gleaned from examining the code itself.

var pluginLabelsExtension = {
    afterDatasetsUpdate(chart){
        if(chart._labels && chart._labels.length > 0){
            this._installLabelPatch(chart._labels[0].constructor);
        }
    },
    // Additional functions and code implementation
}
// Additional code snippets included within the extension

$(document).ready(
    function() {
        var data = {...};   // Data object initialization for the Chart.js pie chart
        
        var labels_url = { ... };   // URL mappings for chart labels
        
        var canvas = document.getElementById("myChart");
        // Further chart setup and configurations
      
        // Canvas click event handling for redirection based on selected chart element
    }
);
<canvas id="myChart" style="width: 1000px; height: 1000px"></canvas>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script>
<script src="https://cdn.jsdelivr.net/gh/emn178/chartjs-plugin-labels/src/chartjs-plugin-labels.js"></script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the method to adjust line spacing of hyperlinks in JavaFX?

I am setting up a vbox and inserting hyperlinks into it. Hyperlink clickableString; clickableString = new Hyperlink(); clickableString.setStyle("-fx-text-fill: black;-fx-padding: 0; -fx-line-spacing: 0em;"); //setting the hyperlink color to black. clicka ...

Is Joomla.submitbutton('module.apply') causing the page to refresh?

The <em>Joomla.submitbutton('module.apply')</em> function causes the page to reload. I attempted to use it with ajax, but the page still reloads. Is there a way to utilize this function with ajax without refreshing the page? Thank yo ...

Creating Functional Tabs Using CSS and JavaScript

I've been experimenting with this code snippet, trying to get it to work better. It's still a work in progress as I'm new to this and have only customized it for my phone so far. The issue can be seen by clicking on the Projects and Today ta ...

CSS and jQuery UI URLs are not resolving properly in MVC framework after deployment

Basically, the issue is that the url for an image in jquery-ui.css file does not resolve once the site is deployed. The website is deployed under the default web site in IIS7, causing the browser console to look for the image in a different location than e ...

Underline Rows in CSS

When designing my website, I decided to organize the content into rows using CSS instead of tables. To create a line separating each row, I used height:px; for each row manually. However, this method resulted in the line jumping around inconsistently acr ...

Creating an intricate table layout using AngularJS and the ngRepeat directive

I'm attempting to create a table similar to the one shown in the image below using HTML5. https://i.sstatic.net/DiPaa.png In this table, there is a multi-dimensional matrix with Class A and Class B highlighted in yellow. Each class has three modes ( ...

Python's web scraping with Selenium

I have encountered an issue while trying to extract data from an HTML table. Although I was able to successfully count the rows, the problem arises when I attempt to print the data as it ends up repeating the same row multiple times. Can someone please h ...

flexbox used to create a fixed sidebar on the left

I want to create a fixed left sidebar using flexbox. The sidebar should have its own scroll bar if the content is long, and I don't want it to move when I scroll down the main content. Check out this example for reference: http://codepen.io/tedartus ...

I am currently in the process of creating a website navbar with HTML and Tailwindcss, but unfortunately, I am running into issues with the desired functionality not being

Even after trying the group-hover:block technique, I am still unable to see the dropdown menu when hovering over the solution and resources. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

jQuery - Instances are sharing a variable

I'm currently developing an image slider plugin that functions well with a single instance. However, I encountered an issue when multiple instances are present on the same page - the variables are shared across these instances, specifically currentPos ...

Zero as the result of Jquery selector's length measurement

var app = app || {}; (function ($) { app.initialize = { init: function () { app.initialize.fullScreen(); }, fullScreen: function () { var scrHeight = $window.height(); console.log($fullScreen ...

What is the process for moving the final character to the beginning of a string?

Initially, the last letter in the string is being displayed. How can I rearrange it so that the last character appears first in the value? https://i.stack.imgur.com/uGq6H.jpg contentHtml += "<td rowspan1=\"" + 1 + "\" class=\"" + ( ...

Is it possible to update the text within a button when hovering over it in Angular?

https://i.sstatic.net/ZhNeM.png https://i.sstatic.net/kb670.png I'm looking to update the text displayed inside a button when hovering over it, similar to the examples in these images. I have already implemented the active state, but now I just need ...

Using Ajax BeginForm can result in the sending of multiple requests, even when only a single

Currently, I am developing a .NET ASP.MVC application and facing an issue in one of my views where I have implemented Ajax.BeginForm. In my _Layout.cshtml file, I have included two scripts in the following order: <script src="~/Scripts/jquery-3.1.1. ...

Animate the child element of this selector using jQuery

When using the this selector to animate an image within the li and a elements, I encountered a problem. Here is the Jquery code snippet: jQuery(document).ready(function () { jQuery(".jcarousel-skin-tango li").mouseenter(function () { ...

Discovering the specific element that was clicked from a collection of elements

I'm dealing with a scenario where multiple divs share the same class. There is a dropdown that alters the background color of one of these divs when a certain option is selected (for example, option 2 changes div #2's background color). My dile ...

How come my items are not appearing on my SharePoint list?

I'm currently working on transferring JSON data to a SharePoint list. The JSON is coming from a database with a 500-item limit per request, so I'm using EPOCH time to make multiple calls and gather all the data. While I can successfully retrieve ...

`Issue with input focus in jQuery``

According to the documentation for jQuery's focus() method, the following code should make the input focused (active): $('#id').focus() However, I'm having trouble getting this to work. Even when trying it in the console on this webs ...

The onBlur attribute does not seem to be functioning properly under a specific condition

I've been attempting to customize the appearance of a search box using the onFocus and onBlur attributes, but unfortunately I haven't been able to achieve the desired outcome. Below is my HTML code: <form action="index.php" method="post" clas ...

HTML - Using Tables to Add and Edit Rows

Take a look at this demo on JSFiddle for my project: Demo jsfiddle I'm currently in the process of creating a table with various functionalities. The function "sortTable" is responsible for sorting the table and it's functioning as expected. T ...