Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined #8

Open
emanuele0208 opened this issue May 15, 2018 · 2 comments
Open

Undefined #8

emanuele0208 opened this issue May 15, 2018 · 2 comments

Comments

@emanuele0208
Copy link

I get undefined error

image

@emanuele0208
Copy link
Author

`var chart1 = new Chartist.Bar('.visitors', {

labels: ['2012', '2013', '2014', '2015','2016','2017','2018'],
series: [[5, 10, 15, 20, 25, 30, 35]]
},{
stackBars: true,
donut: true,
axisY: {
labelInterpolationFnc: function(value) {
return (value) + 'k';
},
offset: 100
},
plugins: [
Chartist.plugins.ctBarLabels()
]
}).on('draw', function(data) {
if(data.type === 'bar') {
data.element.attr({
style: 'stroke-width: 25px'
});
}
});`

@emanuele0208
Copy link
Author

emanuele0208 commented May 15, 2018

`(function(window, document, Chartist) {
'use strict';
var defaultOptions = {
// The class name so you can style the text
labelClass: 'ct-bar-label',

// Use this to get the text of the data and you can return your own
// formatted text. For example, for a percentage: 
// {
//  labelInterpolationFnc: function (text) { return text + '%' }
// }
labelInterpolationFnc: Chartist.noop,

// Depending on your font size you may need to tweak these
labelOffset: {
  x: 0,
  y: 0
},

position: {
  x: null,
  y: null
}

};

Chartist.plugins = Chartist.plugins || {};
Chartist.plugins.ctBarLabels = function(options) {

options = Chartist.extend({}, defaultOptions, options);

var positionX = options.position.x || function (data) {
  return ((data.x1 + data.x2) / 2) + options.labelOffset.x;
};

var positionY = options.position.y || function (data) {
  return ((data.y1 + data.y2) / 2) + options.labelOffset.y;
};

return function ctBarLabels(chart) {
  // Since it's specific to bars, verify its a bar chart
  if(chart instanceof Chartist.Bar) {
    chart.on('draw', function(data) {
      // If the data we're drawing is the actual bar, let's add the text
      // inside of it
      if(data.type === 'bar') {
        data.group.elem('text', {
          // This gets the middle point of the bars and then adds the
          // optional offset to them
          x: positionX(data),
          y: positionY(data),
          style: 'text-anchor: middle'
        }, options.labelClass)
          .text(
          options.labelInterpolationFnc(
            // If there's not x (horizontal bars) there must be a y
            data.value.x || data.value.y
          )
        );
      }
    });
  }
};

};

}(window, document, Chartist));`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant