Coder Social home page Coder Social logo

Comments (3)

camheras avatar camheras commented on June 5, 2024

You can already do something like that 😅

var defaultOpts = Chart.defaults.elements["candlestick"];
var chart = new Chart(ctx, {
	type: 'candlestick',
	data: {
		datasets: [{
			label: 'CHRT - Chart.js Corporation',
			data: barData,
			borderColor :{
				up: defaultOpts.color.up,
				down: defaultOpts.color.down,
				unchanged: defaultOpts.color.up
			}
		}]
	}})

Screenshot 2021-12-13 at 10 24 16

from chartjs-chart-financial.

DaveSkender avatar DaveSkender commented on June 5, 2024

This probably works fine in straight JavaScript, but it's choking on the custom candlestick dataset type in Angular/Typescript.

var defaultOpts = Chart.defaults.elements["candlestick"];

myConfig.data = {
  datasets: [
    {
      type: 'candlestick',
      label: 'Price',
      data: price,
      borderColor: {
        up: defaultOpts.color.up,
        down: defaultOpts.color.down,
        unchanged: defaultOpts.color.up
      },
      yAxisID: 'yAxis'
    }
  ]
};
 node_modules/chart.js/types/index.esm.d.ts:1653:3
    1653   borderColor: Color;
           ~~~~~~~~~~~
The expected type comes from property 'borderColor' which is declared here on 
type 'ChartDataset<keyof ChartTypeRegistry, 
(number | ScatterDataPoint | BubbleDataPoint | FinancialDataPoint)[]>' 

from chartjs-chart-financial.

DaveSkender avatar DaveSkender commented on June 5, 2024

WORKAROUND: for anyone using TypeScript, here's how I solved it:

// add new dataset options type for borderColor
type CandlestickDatasetOptions = BarControllerDatasetOptions & {
    borderColor: {
        up: string,
        down: string,
        unchanged: string
    };
}
// custom border colors
const candleOptions = Chart.defaults.elements["candlestick"];

// sets body color defaults
candleOptions.color.up = 'darkGreen';
candleOptions.color.down = 'darkRed';
candleOptions.color.unchanged = '#555555';

// define border and wicks color
// this should set the default, but still have to reapply in dataset (below)
candleOptions.borderColor = {
  up: candleOptions.color.up,
  down: candleOptions.color.down,
  unchanged: candleOptions.color.unchanged
};

// define base datasets
chartConfig.data = {
  datasets: [
    {
      type: 'candlestick',
      label: 'Price',
      data: price,
      yAxisID: 'yAxis',
      borderColor: candleOptions.borderColor // sets border and wicks color
    }
  ]
};

image

See PR (#115) for my suggestion on how to fix and use this in the main package.

from chartjs-chart-financial.

Related Issues (20)

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.