How to place static pointDots and make vertical lines in Chart.js


Worstein Azatian

js community,

I'm trying to make some customizations to Chart.js, but I'm running into some basic issues...

In this photo you can see what I have achieved

enter image description here

My question is how to set fixed points in the chart? Is there any way to do what I'm trying {showXLabels: 10} as suggested on Chartjs line chart here to limit the number of labels

So far I can't find any logical solution to do this, I've also tried modifying the library directly but without any luck so far.

What I want to achieve is

enter image description here

I was wondering if anyone could suggest me something clever?

or other library I can use to make the second graph work

potato skins

enter image description here


Draw marked vertical lines and marked points

Just expand the chart (actually, if you don't need tooltips, you can do the same thing in the onAnimationCompletecallback )

var data = {
    labels: ['0%', '10%', '20%', '30%', '40%', '50%', '60%', '70%', '80%', '90%', '100%'],
    datasets: [{
        strokeColor: 'rgba(0, 190, 242, 1)',
        fillColor: 'rgba(177, 235, 250, 1)',
        data: [12, 30, 20, 10, 30, 50, 70, 76, 80, 90, 95]
    }]
};


var ctx = document.getElementById("LineExt").getContext("2d");
Chart.types.Line.extend({
    name: "LineExt",
    initialize: function () {
        Chart.types.Line.prototype.initialize.apply(this, arguments);

        // hide alternate x labels on the scale
        var xLabels = this.scale.xLabels;
        xLabels.forEach(function (xLabel, i) {
            if (i % 2)
                xLabels[i] = '';
        })
    },
    draw: function () {
        Chart.types.Line.prototype.draw.apply(this, arguments);

        var scale = this.scale;
        var points = this.datasets[0].points;
        var ctx = this.chart.ctx;
        var options = this.options;

        ctx.save();
        var y0 = scale.calculateY(0);
        this.options.points.forEach(function (point) {
            // linearly extrapolate y value from nearby points
            point.y = (points[Math.floor(point.xIndex)].value + points[Math.ceil(point.xIndex)].value) / 2;

            var x = scale.calculateX(point.xIndex);
            var y = scale.calculateY(point.y);

            // draw the circle
            ctx.beginPath();
            ctx.arc(x, y, 5, 0, 2 * Math.PI);
            ctx.fillStyle = point.color;
            ctx.fill();

            if (point.dropLine) {
                ctx.beginPath();
                ctx.moveTo(x, y);
                ctx.lineTo(x, y0);
                ctx.setLineDash([5, 5]);
                ctx.lineWidth = 2;
                ctx.strokeStyle = point.color;
                ctx.stroke();

                // use the same function that chart.js uses to draw the tooltip to draw our label
                var text = Chart.helpers.template(options.boxTemplate, { value: point.y })
                var boxWidth = ctx.measureText(text).width + 2 * options.tooltipXPadding;
                var boxHeight = options.tooltipFontSize + 2 * options.tooltipYPadding;
                Chart.helpers.drawRoundedRectangle(ctx, x - boxWidth / 2, y - boxHeight - options.tooltipCaretSize, boxWidth, boxHeight, options.tooltipCornerRadius);
                ctx.fill();
                ctx.fillStyle = '#fff';
                ctx.textAlign = 'center';
                ctx.textBaseline = 'middle';
                ctx.fillText(text, x, y - boxHeight / 2 - options.tooltipCaretSize);
            }
        })
        ctx.restore();
    }
});

new Chart(ctx).LineExt(data, {
    scaleLabel: "<%=''%>",
    tooltipTemplate: "<%if (label){%><%=label%>: <%}%><%= value %>",
    scaleFontColor: 'rgba(193, 193, 193, 1)',
    pointDot: false,
    bezierCurve: false,
    scaleOverride: true,
    scaleSteps: 10,
    scaleStepWidth: 10,
    scaleStartValue: 0,
    datasetStrokeWidth: 5,
    points: [
        { xIndex: 4.5, color: 'rgba(0, 190, 242, 1)' },
        { xIndex: 6.5, color: 'rgba(208, 84, 25, 1)' },
        { xIndex: 8, color: 'rgba(199, 0, 160, 1)', dropLine: true }
    ],
    boxTemplate: "<%=value%>%"
});

Fiddle - http://jsfiddle.net/4jbdy2d7/

Related


How to place static pointDots and make vertical lines in Chart.js

Worstein Azatian js community, I'm trying to make some customizations to Chart.js, but I'm running into some basic issues... In this photo you can see what I have achieved My question is how to set fixed points in the chart? Is there any way to do what I'm try

How to place static pointDots and make vertical lines in Chart.js

Worstein Azatian js community, I'm trying to make some customizations to Chart.js, but I'm running into some basic issues... In this photo you can see what I have achieved My question is how to set fixed points in the chart? Is there any way to do what I'm try

How to make two lines thicker in Chart JS

Stijn Westerhof: As you can see in the fiddle , I have used Chart JS to make the chart . There are three lines in this chart. I'm going to make the orange and yellow lines thicker than they are. The green dotted line is good. I searched everywhere and tried a

How to make two lines thicker in Chart JS

Stijn Westerhof: As you can see in the fiddle , I have used Chart JS to make the chart . There are three lines in this chart. I'm going to make the orange and yellow lines thicker than they are. The green dotted line is good. I searched everywhere and tried a

How to make two lines thicker in Chart JS

Stijn Westerhof As you can see in my fiddle , I have made the chart using Chart JS . There are three lines in this chart. I'm going to make the orange and yellow lines thicker than they are. The green dotted line means it is correct. I searched everywhere and

How to make two lines thicker in Chart JS

Stijn Westerhof: As you can see in the fiddle , I have used Chart JS to make the chart . There are three lines in this chart. I'm going to make the orange and yellow lines thicker than they are. The green dotted line is good. I searched everywhere and tried a

How to make Chart.js bar chart stay in place

Will I'm new to chart.js and I'm having trouble rendering it to the space I want. In short, I want it to have other ideas within the designated area. When I render it, it takes up more vertical space than I want and pushes important information off the screen.

How to make Chart.js bar chart stay in place

Will I'm new to chart.js and I'm having trouble rendering it to the space I want. In short, I want it to have other ideas within the designated area. When I render it, it takes up more vertical space than I want and pushes important information off the screen.

How to make Chart.js bar chart stay in place

Will I'm new to chart.js and I'm having trouble rendering it to the space I want. In short, I want it to have other ideas within the designated area. When I render it, it takes up more vertical space than I want and pushes important information off the screen.

How to make Chart.js bar chart stay in place

Will I'm new to chart.js and I'm having trouble rendering it to the space I want. In short, I want it to have other ideas within the designated area. When I render it, it takes up more vertical space than I want and pushes important information off the screen.

How to make Chart.js bar chart stay in place

Will I'm new to chart.js and I'm having trouble rendering it to the space I want. In short, I want it to have other ideas within the designated area. When I render it, it takes up more vertical space than I want and pushes important information off the screen.

How to make a vertical line chart?

Eagle 81 Here is a small example of the data: data <- read.table(header=TRUE, text=' height measurement temperature h1 m1 24.5 h2 m1 24.0 h3 m1 25.0 h4 m1 16.0 h1 m2 22.0 h2 m2 25.0 h3 m2 23.5 h4 m2 17.0 h1 m3 23.2 h2 m3 24.2 h3 m3 23.0 h

How to make a vertical line chart?

Eagle 81 Here is a small example of the data: data <- read.table(header=TRUE, text=' height measurement temperature h1 m1 24.5 h2 m1 24.0 h3 m1 25.0 h4 m1 16.0 h1 m2 22.0 h2 m2 25.0 h3 m2 23.5 h4 m2 17.0 h1 m3 23.2 h2 m3 24.2 h3 m3 23.0 h

Chart.js — draw arbitrary vertical lines

Frederick: How to draw a vertical line at a specific point on the x-axis using Chart.js? In particular, I want to draw a line on the LineChart to indicate the current day. Here is a mockup of the graph : http://i.stack.imgur.com/VQDWR.png Potato Skins: UPDATE

Chart.js 2.0 - Vertical Lines

Wannen Can anyone tell me how to extend Chart.js v2.0. I need vertical lines in a line chart and want to achieve something similar to http://jsfiddle.net/dbyze2ga/ . Chart.types.Line.extend({ name: "LineWithLine", draw: function () { Chart.types.Line.proto

Chart.js — draw arbitrary vertical lines

Frederick: How to draw a vertical line at a specific point on the x-axis using Chart.js? In particular, I want to draw a line on the LineChart to indicate the current day. Here is a mockup of the graph : http://i.stack.imgur.com/VQDWR.png Potato Skins: UPDATE

Chart.js 2.0 - Vertical Lines

Wannen Can anyone tell me how to extend Chart.js v2.0. I need vertical lines in a line chart and want to achieve something similar to http://jsfiddle.net/dbyze2ga/ . Chart.types.Line.extend({ name: "LineWithLine", draw: function () { Chart.types.Line.proto

Chart.js 2.0 - Vertical Lines

Wannen Can anyone tell me how to extend Chart.js v2.0. I need vertical lines in a line chart and want to achieve something similar to http://jsfiddle.net/dbyze2ga/ . Chart.types.Line.extend({ name: "LineWithLine", draw: function () { Chart.types.Line.proto

Chart.js — draw arbitrary vertical lines

Frederick: How to draw a vertical line at a specific point on the x-axis using Chart.js? In particular, I want to draw a line on the LineChart to indicate the current day. Here is a mockup of the graph : http://i.stack.imgur.com/VQDWR.png Potato Skins: UPDATE

Chart.js 2.0 - Vertical Lines

Wannen Can anyone tell me how to extend Chart.js v2.0. I need vertical lines in a line chart and want to achieve something similar to http://jsfiddle.net/dbyze2ga/ . Chart.types.Line.extend({ name: "LineWithLine", draw: function () { Chart.types.Line.proto

Chart.js 2.0 - Vertical Lines

Wannen Can anyone tell me how to extend Chart.js v2.0. I need vertical lines in a line chart and want to achieve something similar to http://jsfiddle.net/dbyze2ga/ . Chart.types.Line.extend({ name: "LineWithLine", draw: function () { Chart.types.Line.proto

Chart.js 2.0 - Vertical Lines

Wannen Can anyone tell me how to extend Chart.js v2.0. I need vertical lines in a line chart and want to achieve something similar to http://jsfiddle.net/dbyze2ga/ . Chart.types.Line.extend({ name: "LineWithLine", draw: function () { Chart.types.Line.proto

Chart.js 2.0 - Vertical Lines

Wannen Can anyone tell me how to extend Chart.js v2.0. I need vertical lines in a line chart and want to achieve something similar to http://jsfiddle.net/dbyze2ga/ . Chart.types.Line.extend({ name: "LineWithLine", draw: function () { Chart.types.Line.proto

How to make vertical lines in HTML

gopal How to make vertical lines using HTML? Chris Vandermaster <div>Put a tag around the tag you want to appear on the line below it , and style it with CSS: .verticalLine { border-left: thick solid #ff0000; } <div class="verticalLine"> some other content