gpt4 book ai didi

reactjs - Apex 图表不会立即显示

转载 作者:行者123 更新时间:2023-12-04 03:47:17 24 4
gpt4 key购买 nike

我有以下代码用于根据值显示顶点图表

import React, { Component, Fragment,useState } from "react";

import RestAPI from "services/api";
import axios from "axios";
import Select from 'react-select';
import "d3-transition";
import "tippy.js/dist/tippy.css";
import "tippy.js/animations/scale.css";


/* Chart code */
// Themes begin
// Themes end
import {
Button,
Label,
FormGroup,
Form,

} from "reactstrap";
import ReactApexChart from "react-apexcharts";
import Loader from "react-loader-spinner";

class BarChart extends Component {
constructor(props){
super(props);
this.selectValue=this.selectValue.bind(this)
this.state = {
selectValue:"",
items:[],
weights:[],
isLoaded:true,
showViz:false,

series: [],
options: "",


};
}





selectValue (e) {
var selectValue=this.state.selectValue;
selectValue=e.value;
fetch("http://127.0.0.1:8000/api/values/"+selectValue)
.then(response => response.json())
.then(json => {
this.state.series=[]
var {items,weights}=this.state;
this.setState({
isLoaded:true,
items:json.keywords,
weights:json.weights,

series: [{ name: "Keywords", data: weights }],
options:{
chart: {
type: 'bar',

},
title:{
text:"Top 10 values"
},
plotOptions: {
bar: {
horizontal: true,
}
},
xaxis:{
categories: items
},
grid: {
xaxis: {
show:false,
lines: {
show: false
},
axisTicks:{
show:false,
offsetX: 0,
offsetY: 0
},
axisBorder:{
show:false
}
}
},
yaxis: {
reversed: false,
axisTicks: {
show: false
}
}
},



})
});
this.state.showViz=true;
}



render() {

var {selectValue,items,weights,isLoaded,options,series,showViz}=this.state;


const yeardata = [
{
value: "1",
label: "1"
},
{
value: "2",
label: "2"
},
{
value: "3",
label: "3"
},
{
value: "4",
label: "4"
},
{
value: "5",
label: "5"
},
{
value: "6",
label: "6"
},
{
value: "7",
label: "7"
},
{
value: "8",
label: "8"
},
{
value: "9",
label: "9"
},
{
value: "10",
label: "10"
}
];





//var{items,arr_keys,arr_vals}=this.state;






if(isLoaded){

return (
<>

{ console.log("the values are:",items)}
{ console.log("the values are:",weights)}
{ console.log("the values are:",options)}
{ console.log("the values are:",series)}
<Form role="form" >


<FormGroup>
<h2>Top 10 values</h2>
<Label>Select an Value</Label>
<Select placeholder="Select Option" options={yeardata} value={yeardata.find(obj => obj.value === selectValue)}
onChange={this.selectValue}
/>

<br></br>
{this.state.showViz?(
<ReactApexChart options={this.state.options} series={this.state.series} type="bar" height={250} />):
(<Loader type="Puff">
</Loader>)}
</FormGroup>
</Form>



</>
);


}
else{
return(
<>

</>
)
}

}
}

export default BarChart;

我想在选择一个值时显示图表,但它没有显示。相反,如果我选择另一个值,则会显示以前的值。另外,最初呈现一个我不想要的空白图。也,加载程序会在页面加载后立即显示,我也想避免这种情况。我该如何解决这个问题?渲染图截图: Empty Chart

最佳答案

  1. 您永远不会将 isLoaded 设置为 false,因此它始终为 true。您需要在构造函数中将 isLoaded 设置为 false 并在收到响应后 - true:

  2. React 状态是异步的,你不能像这样设置一个值 this.state.showViz=true; ,你需要使用 this.setState 每次你想改变你的状态时,不要忘记把所有其他状态数据放在一起

    this.setState({...this.state, showViz: true});

而且我已经正确理解了您的逻辑,您需要将此代码放在 last.then 中:

this.setState({
isLoaded:true,
items:json.keywords,
weights:json.weights,
showViz: true,
...
  1. 您从未设置 selectValue,因此不需要此代码 var selectValue=this.state.selectValue

最终代码:

import React, { Component, Fragment, useState } from "react";

import RestAPI from "services/api";
import axios from "axios";
import Select from "react-select";
import "d3-transition";
import "tippy.js/dist/tippy.css";
import "tippy.js/animations/scale.css";

/* Chart code */
// Themes begin
// Themes end
import { Button, Label, FormGroup, Form } from "reactstrap";
import ReactApexChart from "react-apexcharts";
import Loader from "react-loader-spinner";

class BarChart extends Component {
constructor(props) {
super(props);
this.selectValue = this.selectValue.bind(this);
this.state = {
selectValue: "",
items: [],
weights: [],
isLoaded: true,
showViz: false,

series: [],
options: "",
};
}

selectValue(e) {
var selectValue = this.state.selectValue;
selectValue = e.value;
fetch("http://127.0.0.1:8000/api/values/" + selectValue)
.then((response) => response.json())
.then((json) => {
var { items, weights } = this.state;

this.setState({
isLoaded: true,
items: json.keywords,
weights: json.weights,
showViz: true,
series: [
{
name: "Keywords",
data: weights,
},
],
options: {
chart: {
type: "bar",
},
title: {
text: "Top 10 values",
},
plotOptions: {
bar: {
horizontal: true,
},
},
xaxis: {
categories: items,
},
grid: {
xaxis: {
show: false,
lines: {
show: false,
},
axisTicks: {
show: false,
offsetX: 0,
offsetY: 0,
},
axisBorder: {
show: false,
},
},
},
yaxis: {
reversed: false,
axisTicks: {
show: false,
},
},
},
});
});
}

render() {
var {
selectValue,
items,
weights,
isLoaded,
options,
series,
showViz,
} = this.state;

const yeardata = [
{
value: "1",
label: "1",
},
{
value: "2",
label: "2",
},
{
value: "3",
label: "3",
},
{
value: "4",
label: "4",
},
{
value: "5",
label: "5",
},
{
value: "6",
label: "6",
},
{
value: "7",
label: "7",
},
{
value: "8",
label: "8",
},
{
value: "9",
label: "9",
},
{
value: "10",
label: "10",
},
];

if (isLoaded) {
return (
<>
{console.log("the values are:", items)}
{console.log("the values are:", weights)}
{console.log("the values are:", options)}
{console.log("the values are:", series)}
<Form role="form">
<FormGroup>
<h2> Top 10 values </h2> <Label> Select an Value </Label>
<Select
placeholder="Select Option"
options={yeardata}
value={yeardata.find((obj) => obj.value === selectValue)}
onChange={this.selectValue}
/>
</br>
{this.state.showViz ? (
<ReactApexChart
options={this.state.options}
series={this.state.series}
type="bar"
height={250}
/>
) : (
<Loader type="Puff"></Loader>
)}
</FormGroup>
</Form>
</>
);
} else {
return <></>;
}
}
}

关于reactjs - Apex 图表不会立即显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64956061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com