GithubHelp home page GithubHelp logo

flot-labels's People

Contributors

burlandm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

flot-labels's Issues

%y not working on pie chart

Hello,

I cant seem to get %y working on my flot pie chart, it just hows %y. it works fine on my line chart.

Several issues and an improved version

  • Multiple axes are not working correctly: Coordinates for axis 2+ are wrong
  • In HTML mode, the labels are not removed once the plot is panned or zoomed (they will sum up until the browser gives up)
  • Bounds in canvas mode are useless

Instead of forking or doing a pull request I will just post my fixed "canvas-only"-version here. Maybe it will be helpful.

/*
* The MIT License
Copyright (c) 2012 by Matt Burland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/*
"jquery.flot.labels2" Version 1.0 - Copyright (c) 2016 by Tobias Jüttner.
Inspired by "jquery.flot.labels" 0.2 - Copyright (c) 2012 by Matt Burland.

This plugin will draw labels next to the plotted points on your graph.

usage -
<script type="text/javascript">
    var names = ["foo","bar"];
    var data = { data: [[1,1],[2,2]], showLabels: true, labels: names, labelPlacement: "left" };
    $.plot($("#placeholder), [data], options);
</script>

For each series that you want labeled you need to set "showLabels" to true, set labels to an array of label names (strings)
and optionally set labelPlacement to one of "left", "right", "above" or "below" (below by default if not specified).
For the names, the array must not be larger than the length of the data. If any are missing (null) then the label for that point will
be skipped. For example, to label only the 1st and 3rd points:

    var names = ["foo", null, "bar"];

Labels are always drawn via canvas.
Additional options for series (with defaults):

    cColor: "#000",           // color for the text
    cFont: "9px sans-serif",  // font for the text
    cPadding: 4               // padding to add (where padding is added depends on "labelPlacement")
*/

(function ($) {

    function init(plot) {
        plot.hooks.drawSeries.push(drawSeries);
    }

    function drawSeries(plot, ctx, series) {
        if (!series.showLabels || !series.labels || series.labels.length == 0) {
            return;
        }
        ctx.save();
        ctx.translate(plot.getPlotOffset().left, plot.getPlotOffset().top);
        ctx.fillStyle = series.cColor;
        ctx.font = series.cFont;

        for (var i = 0; i < series.labels.length; i++) {
            if (series.labels[i]) {
                // Inspired by "drawSeriesLines"
                var points = series.datapoints.points
                var x = points[i * series.datapoints.pointsize];
                var y = points[i * series.datapoints.pointsize + 1];
                if (x >= series.xaxis.min && x <= series.xaxis.max && y >= series.yaxis.min && y <= series.yaxis.max)
                    drawLabel(series.labels[i], series.xaxis.p2c(x), series.yaxis.p2c(y));
            }
        }

        ctx.restore();

        function drawLabel(contents, x, y) {
            var radius = series.points.radius;
            var tWidth = ctx.measureText(contents).width;
            switch (series.labelPlacement) {
                case "above":
                    x = x - tWidth / 2;
                    y -= (series.cPadding + radius);
                    ctx.textBaseline = "bottom";
                    break;
                case "left":
                    x -= tWidth + series.cPadding + radius;
                    ctx.textBaseline = "middle";
                    break;
                case "right":
                    x += series.cPadding + radius;
                    ctx.textBaseline = "middle";
                    break;
                default:
                    ctx.textBaseline = "top";
                    y += series.cPadding + radius;
                    x = x - tWidth / 2;

            }
            ctx.fillText(contents, x, y);
        }
    }

    // labelPlacement options: below, above, left, right
    var options = {
        series: {
            showLabels: false,
            labels: [],
            labelClass: null,
            labelPlacement: "below",
            cColor: "#000",
            cFont: "9px sans-serif",
            cPadding: 4
        }
    };

    $.plot.plugins.push({
        init: init,
        options: options,
        name: "seriesLabels",
        version: "1.0"
    });
})(jQuery);

Incorrect label positioning with stacked charts

When implemented on a stacked bar chart, the labels aren't positioned correctly. The labels for both series are placed where they would be if they started at the bottom. This means the labels on the top bar are too low. For simplicity, the example screenshot has the same values for each series so they appear together. The labels are also BEHIND the bars.

screen shot 2016-08-30 at 14 23 01

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Labels Example</title>


    <script type='text/javascript' src="/assets/js/jquery-2.1.3.min.js"></script>
    <script type='text/javascript' src='/assets/plugins/charts-flot/jquery.flot.min.js'></script>
    <script type="text/javascript" src='/assets/plugins/charts-flot/jquery.flot.labels.js'></script>
    <script type='text/javascript' src='/assets/plugins/charts-flot/jquery.flot.stack.min.js'></script>

    <script type="text/javascript">
$(function () {

            var options = {
                axisLabels: {show: true},
                series: {
                    stack: true,
                    bars: {
                        show: true,
                        barWidth: 0.9,
                        lineWidth: 0,
                        align: "center",
                        order:1
                    }
                },
                colors: ["#ad1986", "#2ea5b4"]
            };

            var data = [
                {
                    points: { show: true },
                    showLabels: true,
                    labels: ["Alpha","Beta","Gamma","Delta","Epsilon","Zeta","Eta","Theta"],
                    labelPlacement: "left",
                    canvasRender: true,
                    cColor: "#AD8200",
                    data:[ [1, 44],[2, 64],[3, 422],[4, 591],[5, 428],[6, 317],[7, 377],[8, 484]]
                },

                {
                    points: { show: true },
                    showLabels: true,
                    labels: ["Alpha","Beta","Gamma","Delta","Epsilon","Zeta","Eta","Theta"],
                    labelPlacement: "right",
                    canvasRender: true,
                    cColor: "#AD8200",
                    data:[ [1, 44],[2, 64],[3, 422],[4, 591],[5, 428],[6, 317],[7, 377],[8, 484]]
                }
            ];

            $.plot($("#placeholder"), data, options);
        });
    </script>
</head>
<body>
<div id="placeholder" style="width:500px; height:350px"></div>

</body>
</html>

having trouble with labels not showing up

especially the last label on the right
and some others near the axis

would like to see some boundary checking and adjustment

thank you for the excellent Flot plugin

Font size

Hi,

How can I change the font size of the labels? I have tried setting the css and changing cFont in the plots, but it doesn't seem to change anything.

added ALT labelPlacement

Alternates the labels between above and below. Useful for tight graphs

                case "alt":
                    if (i % 2) {
                        // above
                        x = x - tWidth / 2;
                        y -= (series.cPadding + radius);
                        ctx.textBaseline = "bottom";
                        break;
                        } else {
                        //below
                        ctx.textBaseline = "top";
                        y += series.cPadding + radius;
                        x = x - tWidth / 2;
                        break;
                        };

Need a way to clear the labels

I have a plot where I redraw the chart with a different series of points based on a user selection. I need a way to clear off all the labels that were already there and replace them with new ones. Is that a simple change?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.