How to set the chart pie color in correct orders?

Viewed 186

I am working on chart pie to input the data and I want to make the correct order for the chart pie color, I want to make the green pie to go on the left and the blue pie to go on the right.

var showalert = 'opened';
var opened_today = 2;
var clicked_today = 1;

var ctx = document.getElementById("myPieChart");
var myPieChart = new Chart(ctx, {
  type: 'pie',
  legend:{
    position: 'right'
  },
  
  data: {
    labels: ["Opened", "Clicked"],

    datasets: [{
        data: [
            opened_today,
            clicked_today
        ],
        backgroundColor: [
            "#28a745",
            "#007bff"
        ],
    }],
  },
});
.chartjs-render-monitor {
    animation: chartjs-render-animation 1ms;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment-with-locales.min.js" integrity="sha256-2upzq+m3oG9Q4Xye6pGvLrXgrzOKtTgR1D2GCLUzL2o=" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://code.iconify.design/1/1.0.7/iconify.min.js"></script>

<script src="https://startbootstrap.github.io/startbootstrap-sb-admin-2/vendor/chart.js/Chart.min.js"></script>


<!-- Custom fonts for this template-->
<link href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet">

<!-- Custom styles for this template-->
<link href="https://startbootstrap.github.io/startbootstrap-sb-admin-2/css/sb-admin-2.min.css" rel="stylesheet">


<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Card Body -->
<div class="card-body" style="height: 401px;">
    <div class="chart-pie pt-4 pb-2">

      <canvas id="myPieChart" width="272" height="245" class="chartjs-render-monitor" style="display: block; width: 272px; height: 245px; text-align: right;"></canvas>
    </div>
</div>

Here is what my chart pie show:

https://i.imgur.com/QeMIon8.png

When I try this:

var myPieChart = new Chart(ctx, {
  type: 'pie',
  data: {
    labels: ["Opened", "Clicked"],

    datasets: [{
        data: [
            
            clicked_today,
            opened_today
        ],
        backgroundColor: [
            
            "#007bff",
            "#28a745"
        ],
    }],
  },
});

I am getting this:

chart 3.png

Here is what I want to achieve:

chart2.png

Can you please show me an example how I can make the green pie to go on the left and the blue pie to go on the right?

Thank you.

1 Answers

Ok, you have to choose your data/color following clockwise, first clicked/blue then opened/green.

All settings for legend have to be inside key options

For legend, reverse does the job

var showalert = 'opened';
var opened_today = 2;
var clicked_today = 1;

var ctx = document.getElementById("myPieChart");
var myPieChart = new Chart(ctx, {
  type: 'pie',
  options: {
    legend: {
      reverse:true,
      position: 'top',
      labels: {
        fontColor: 'gray'
      }
    }
  },
  data: {
    labels: ["Clicked","Opened"],
    datasets: [{
      data: [
        clicked_today,
        opened_today,
      ],
      backgroundColor: [
        "#007bff", //blue
        "#28a745", //green,
      ],
    }],
  },
});
.chartjs-render-monitor {
  animation: chartjs-render-animation 1ms;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment-with-locales.min.js" integrity="sha256-2upzq+m3oG9Q4Xye6pGvLrXgrzOKtTgR1D2GCLUzL2o=" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://code.iconify.design/1/1.0.7/iconify.min.js"></script>

<script src="https://startbootstrap.github.io/startbootstrap-sb-admin-2/vendor/chart.js/Chart.min.js"></script>


<!-- Custom fonts for this template-->
<link href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet">

<!-- Custom styles for this template-->
<link href="https://startbootstrap.github.io/startbootstrap-sb-admin-2/css/sb-admin-2.min.css" rel="stylesheet">


<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Card Body -->
<div class="card-body" style="height: 401px;">
  <div class="chart-pie pt-4 pb-2">

    <canvas id="myPieChart" width="272" height="245" class="chartjs-render-monitor" style="display: block; width: 272px; height: 245px; text-align: right;"></canvas>
  </div>
</div>

Related