question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

border around a chart

See original GitHub issue

How to draw a border around a chart?

According to https://github.com/rrag/react-stockcharts/issues/310, I should change border with

.react-stockcharts-grabbing-cursor,
.react-stockcharts-crosshair-cursor

However, looking into html, a chart is created with “g” without a css class attached. I want to have a border-bottom at the end of each chart. So actually, I can use <XAxis axisAt="bottom" orient="bottom" showTicks="false"/> to simulate a border at the bottom of a chart.

I want to use something simpler:

border-bottom: 1px solid black;

How can I do this?

The “Chart” has no property such as “borderStyle”.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
cbrwizardcommented, May 3, 2018

Ended up creating a custom component for that purpose:

import React, { Component } from 'react'
import PropTypes from 'prop-types'
import {
  hexToRGBA,
  strokeDashTypes,
  getStrokeDasharray,
} from 'react-stockcharts/lib/utils'
import GenericChartComponent from 'react-stockcharts/lib/GenericChartComponent'
import { getAxisCanvas } from 'react-stockcharts/lib/GenericComponent'

class ChartHorizontalLine extends Component {
  constructor(props) {
    super(props)
    this.renderSVG = this.renderSVG.bind(this)
    this.drawOnCanvas = this.drawOnCanvas.bind(this)
  }

  drawOnCanvas(ctx) {
    const {
      stroke,
      strokeWidth,
      opacity,
      strokeDasharray,
      width,
      y,
      x,
    } = this.props
    ctx.beginPath()

    ctx.strokeStyle = hexToRGBA(stroke, opacity)
    ctx.lineWidth = strokeWidth

    const y1 = y
    const y2 = y
    const x1 = x
    const x2 = x + width

    ctx.setLineDash(getStrokeDasharray(strokeDasharray).split(','))
    ctx.moveTo(x1, y1)
    ctx.lineTo(x2, y2)
    ctx.stroke()
  }

  renderSVG() {
    const {
      width,
      className,
      x,
      y,
      stroke,
      strokeWidth,
      opacity,
      strokeDasharray,
    } = this.props

    return (
      <line
        className={className}
        strokeDasharray={getStrokeDasharray(strokeDasharray)}
        stroke={stroke}
        strokeWidth={strokeWidth}
        strokeOpacity={opacity}
        y1={y}
        y2={y}
        x1={x}
        x2={x + width}
      />
    )
  }

  render() {
    const { clip } = this.props

    return (
      <GenericChartComponent
        svgDraw={this.renderSVG}
        clip={clip}
        canvasDraw={this.drawOnCanvas}
        canvasToDraw={getAxisCanvas}
        drawOn={['pan']}
      />
    )
  }
}

ChartHorizontalLine.propTypes = {
  className: PropTypes.string,
  /* eslint-disable react/boolean-prop-naming */
  clip: PropTypes.bool,
  stroke: PropTypes.string,
  strokeWidth: PropTypes.number,
  strokeDasharray: PropTypes.oneOf(strokeDashTypes),
  opacity: PropTypes.number,
  y: PropTypes.number.isRequired,
  x: PropTypes.number.isRequired,
  width: PropTypes.number.isRequired,
}

ChartHorizontalLine.defaultProps = {
  className: 'line ',
  clip: true,
  stroke: '#000000',
  opacity: 0.5,
  strokeWidth: 1,
  strokeDasharray: 'Solid',
}

export default ChartHorizontalLine

@rrag do you want me to open a pr with this? I can name it HorizontalLine.

1reaction
cbrwizardcommented, Apr 23, 2018

Turned out it’s fixable by passing clip={false} to GenericChartComponent rendered in StraightLine. Opened a pr for it https://github.com/rrag/react-stockcharts/pull/538

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Make a Border Around a Graph in Excel
Hover over the Dashes option and choose a style for the border, such as dashed or dotted lines. You can also hover over...
Read more >
Apply or remove cell borders on a worksheet - Microsoft Support
By using predefined border styles, you can quickly add a border around cells or ranges of cells.
Read more >
Add a border and background to a chart in Pages on Mac
You can add a border and a background color to any 2D bar, column, area, or scatter chart. A 2D column chart with...
Read more >
Chart Area Border | Chart.js
Chart Area Border. config plugin data. const config = { type: 'line', data: data, options: { plugins: { chartAreaBorder: { borderColor: ...
Read more >
How to Add/Remove Chart Border in Excel
Step 1: Click on the border, right click to load menu, select 'Format Chart Area'. ... Step 2: On Format Chart Area, under...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found