reactjs - 如何在 react 图表的两个轴上显示文本?

标签 reactjs charts bar-chart pie-chart linechart

我刚开始 react ,我想创建一个图表来显示技能水平。我想在 X 轴和 Y 轴上显示文本的地方。我使用 React.js 创建了条形图/折线图和饼图。目前我能够在 X 轴上显示数字和文本。但是,有没有办法在两个轴上显示字符串(文本)?

我想显示 [“专家”、“高级”、“中级”、“初学者”] 在 Y 轴和 X 轴上,我想显示 - [“HTML”、“CSS”、“React JS”、“Javascript”、“SQL”、“德鲁巴”]

我正在使用“react-chartjs-2”模块( https://gor181.github.io/react-chartjs-2/ )。

我创建了名为“Chart.jsx”的组件,其代码是:

import React, { Component } from "react";
import { Bar, Line, Pie } from "react-chartjs-2";
import "./Chart.css";

class Chart extends Component {
  constructor(props) {
    super(props);
    this.state = {
      chartData: props.chartData

    };
  }

  static defaultProps = {
    displayTitle: true,
    DisplayLegend: true,
    LegendPosition: 'right',
    level:'Skills'
  };

  render() {
    return (
      <div className="chart">
        <Bar
          data={this.state.chartData}
          options={{
            title: {
              display: this.props.displayTitle,
              text: 'Skills I am proficient with '+this.props.level,
            },
            legend: {
              display: this.props.DisplayLegend,
              position: this.props.LegendPosition
            }
          }}
        />

        <Line
          data={this.state.chartData}
          options={{
            title: {
              display: this.props.displayTitle,
              text: 'Skills I am proficient with '+this.props.level,
            },
            legend: {
              display: this.props.DisplayLegend,
              position: this.props.LegendPosition
            }
          }}
        />

        <Pie
          data={this.state.chartData}
          options={{
            title: {
              display: this.props.displayTitle,
              text: 'Skills I am proficient with '+this.props.level,
            },
            legend: {
              display: this.props.DisplayLegend,
              position: this.props.LegendPosition
            }
          }}
        />
      </div>
    );
  }
}

export default Chart;

我创建了一个名为“Skills.jsx”的页面,其代码是:
import React, { Component } from "react";
import Navbar from "../components/Navbar.jsx";
import Footer from "../components/Footer.jsx";
import Jumbotron from "../components/Jumbotron.jsx";
import Chart from "../components/Chart.jsx";

class Skills extends Component {
  constructor() {
    super();
    this.state = {
      chartData: {}
    };
  }

  componentWillMount() {
    this.getChartData();
  }

  getChartData() {
    //Ajax calls here
    this.setState({
      chartData: {
        labels: [
          "HTML",
          "CSS",
          "React JS",
          "Javascript",
          "SQL",
          "Drupal"
        ],
        datasets: [
          {
           // labels: "Level",
           labels: ["Expert", "Advanced", "Intermediate", "Beginner"],
           data: [100, 90, 90, 70, 60, 50, 40, 30, 20, 10, 0],
            //labels: ["Expert", "Advanced", "Intermediate", "Beginner"],
            displays: ["Expert", "Advanced", "Intermediate", "Beginner"],
            backgroundColor: [
              "rgba(255, 99, 132, 0.6)",
              "rgba(54, 162, 235, 0.6)",
              "rgba(255, 206, 86, 0.6)",
              "rgba(75, 192, 192, 0.6)",
              "rgba(153, 102, 235, 0.6)",
              "rgba(255, 159, 132, 0.6)",
              "rgba(255, 99, 132, 0.6)"
            ]
          }
        ]
      }
    });
  }

  render() {
    return (
      <div>
        <Navbar />
        <Jumbotron title="welcome" subtitle="put something" />
        <div className="container">
          <h2>My Skills</h2>
          <p>Look, what I can do ..</p>
</div>
<div>
          <Chart chartData={this.state.chartData} lavel="HTML" LegendPosition="bottom" />
        </div>
        <Footer />
      </div>
    );
  }
}
export default Skills;

先感谢您!

最佳答案

您需要使用 scales title在选项里面

options = {
  scales: {
    yAxes: [{
      scaleLabel: {
        display: true,
        labelString: 'Y text'
      }
    }],
    xAxes: [{
      scaleLabel: {
        display: true,
        labelString: 'X text'
      }
    }],
  }     
}

关于reactjs - 如何在 react 图表的两个轴上显示文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47117106/

相关文章:

从 ReactJS 客户端调用时,Java Web 服务无法加载

javascript - 滚动到该部分时如何使 Chart.js 动画?

javascript - 动态更改 Google Charts 中的 View 列名称

r - 如何从 Google 表单调查复选框网格问题中绘制多个分类变量并在 R 中进行最佳可视化?

javascript - JqPlot 荧光笔在条形图上错位

javascript - 带字符串插值的 React.js className 值

javascript - ES6中使用Gulp编译React时出现语法错误

javascript - React redux 不同步 mapStateToProps 中的 Prop

javascript - Headless chrome print-to-pdf 错误地显示 Chart.JS javascript Canvas

python - 删除 Seaborn 条形图图例标题