Giter Site home page Giter Site logo

chartist-plugin-barlabels's Introduction

chartist-plugin-barlabels

Build Status NPM Version

Chartist-js Plugin for labelling the end of bars in Bar Charts.

Usage

Default Options

There are two sets of default options depending on if the chart.options has horizontalBars set to true or false.

// if chart.options.horizontalBars == true

var defaultOptions = {
  labelClass: 'ct-label',
  labelInterpolationFnc: Chartist.noop,
  labelOffset: {
    x: 2,
    y: 4
  },
  labelPositionFnc: undefined,
  startAtBase: undefined,
  textAnchor: 'start',
  showZeroLabels: false,
  includeIndexClass: false,
  thresholdPercentage: 30,
  thresholdOptions: {
    belowLabelClass: 'ct-label-below',
    aboveLabelClass: 'ct-label-above'
  }
}
// if chart.options.horizontalBars == false / undefined

var defaultOptions = {
  labelClass: 'ct-label',
  labelInterpolationFnc: Chartist.noop,
  labelOffset: {
    x: 0,
    y: -2
  },
  labelPositionFnc: undefined,
  startAtBase: undefined,
  textAnchor: 'middle',
  showZeroLabels: false,
  includeIndexClass: false,
  thresholdPercentage: 30,
  thresholdOptions: {
    belowLabelClass: 'ct-label-below',
    aboveLabelClass: 'ct-label-above'
  }
}
Label Position Function

The label position function can be used to conditionally influence the location of the label relative to the bar. The function has some bar data being passed to it in this format:

{
  high: number; // the highest value in the chart or the defined high in the options
  value: number; // the value of the bar that the label will belong to
  threshold: number; // the percentage threshold defined in the options
}

The function must return labelOffset and textAnchor data otherwise when this data is undefined it will fall back on the default offsets.

{
  labelOffset: {
    x: number // the offset to the label on the X axis
    y: number // the offset to the label on the Y axis
  },
  textAnchor: string // the css text-anchor value
}

The plugin comes with a default example.

Chartist.plugins.ctBarLabels.InsetLabelsPositionHorizontal = function(data) {

  if (data.high && data.value && data.threshold) {
    var aboveThreshold = (data.value / data.high * 100 > data.threshold);

    if (aboveThreshold) {
      return {
        labelOffset: {
          x: -2,
          y: 4
        },
        textAnchor: 'end'
      }
    } else {
      return {
        labelOffset: {
          x: 2,
          y: 4
        },
        textAnchor: 'start'
      };
    }
  }
};
Example Script
new Chartist.Bar('#test-bar-chart', {
    labels: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
    series: [
      [5, 4, 3, 7, 5, 10, 3],
      [1, 2, 3, 4, 5, 6, 7]
    ]
  }, {
    seriesBarDistance: 10,
    reverseData: true,
    horizontalBars: true,
    axisY: {
      offset: 70
    },
    plugins: [
      Chartist.plugins.ctBarLabels()
    ]
  });
Example Screenshot

Example Graph

chartist-plugin-barlabels's People

Contributors

mtgibbs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

chartist-plugin-barlabels's Issues

error [object object]

hello i would like use this plugin for display data of bar but is display "[object object]

// test

     var datatestsSubscriptionChart = {
      labels: ['TST', 'DEV', 'ASB', 'INT'],
      series: [
        [20, 60, 40, 100]

      ]
    };
    var optionstestsSubscriptionChart = {
    plugins: [

Chartist.plugins.ctBarLabels()

],

    axisX: {
            showGrid: false
        },
        low: 0,
        high: 200,
        chartPadding: { top: 0, right: 5, bottom: 0, left: 0}
    };
    var responsiveOptions = [
      ['screen and (max-width: 640px)', {
        seriesBarDistance: 8,
        axisX: {
          labelInterpolationFnc: function (value) {
            return value[0];
          }
        }
      }]
    ];

    var testsSubscriptionChart = Chartist.Bar('#testsSubscriptionChart', datatestsSubscriptionChart, optionstestsSubscriptionChart, responsiveOptions);

image

can help me ?

Angular component loading issue

Hi,

We are getting this error on page load, npm installed correctly. angular 14+

ROR Error: Uncaught (in promise): TypeError: Cannot set properties of undefined (setting 'returnExportsGlobal')
TypeError: Cannot set properties of undefined (setting 'returnExportsGlobal')
at Object. (chartist-plugin-barlabels.js:21:17)

Question

Hey,

I was wondering if it is possible to position the barlabel above the bar when the value is positive and below the bar when the value is negative.

My code for the bar chart options
plugins: [ Chartist.plugins.ctBarLabels({ textAnchor: 'middle', labelClass: 'ct-label', showZeroLabels: true, labelOffset: { x: 0, y: -15 }, }) ],

positions the label of positive values with an offset of -15 slightly above the bars.

When I have negative values it looks like this:
image

I would like to put the labels below the bars, please consider to add an example code on how to achieve this:

text842

Best regards,
flix

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.