javascript - 不同尺寸的迷你图?

标签 javascript reactjs sparklines

我正在制作一个简单的天气应用程序,我的迷你图显示出不同的尺寸,即使具有相同的高度和宽度属性,知道为什么吗?

天气列表容器:

import React, { Component } from 'react';
import { connect } from 'react-redux';
import Chart from '../components/chart';

class WeatherList extends Component {
    renderWeather(cityData) {
        const name = cityData.city.name;
        const temps = cityData.list.map(weather => weather.main.temp);
        const pressures = cityData.list.map(weather => weather.main.pressure);
        const humidities = cityData.list.map(weather => weather.main.humidity);

        return(
            <tr key={name}>
                <td>{name}</td>
                <td><Chart data={temps} color="orange" units="K" /></td>
                <td><Chart data={humidities} color="green" units="%" /></td>
                <td><Chart data={pressures} color="black" units="hPa" /></td>
            </tr>
        );
    }


    render(){
        return(
          <table className="table table-hover">
            <thead>
                <tr>
                    <th>City</th>
                    <th>Temperature (K)</th>
                    <th>Humidity (%)</th>
                    <th>Pressure (hPa)</th>
                </tr>
            </thead>
            <tbody>
                {this.props.weather.map(this.renderWeather)}
            </tbody>
          </table>  
        );   
    }
}

function mapStateToProps({ weather }) {
    return { weather };
}

// Above function can also be written...
/* function mapStateToProps({state}){
    return {weather: state.weather};
}*/

export default connect(mapStateToProps)(WeatherList);

实际图表组件:

import _ from 'lodash';
import React from 'react';
import { Sparklines, SparklinesLine, SparklinesReferenceLine } from 'react-sparklines';

// lodash will grab the average
function average(data) {
  return _.round(_.sum(data)/(data.length));
}

export default (props) => {
    return (
      <div>
        <Sparklines height={120} width={180} data={props.data}>
            <SparklinesLine color={props.color} />
            <SparklinesReferenceLine type="avg" />
        </Sparklines>
        <div>{average(props.data)} {props.units}</div>
      </div>  
    ); 
};

我认为它可能在 WeatherList 中某处的 JSX 中,但到目前为止我一直坐在这里没有成功。

最佳答案

尝试

高度 -> svgHeight

宽度 -> svgWidth

应该更正你的输出

关于javascript - 不同尺寸的迷你图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45624036/

相关文章:

javascript - 为 JavaScript 中的更多参数扩展函数

javascript - 主干网显示/隐藏渲染 View 最佳实践

javascript - 如何在滚动时正确淡入DIV?

javascript - 使用 this.state.example 太多次,如何将其放入变量中?

jquery Sparkline 没有很好地显示第一个栏

javascript - 用 jquery sparkline 和 mysql 数据绘制条形图

php - 从 php 到 javascript 的 json 对象分配有什么问题?

javascript - 在浏览器上使用 javascript 在 Google API 上发送 post 请求

javascript - 需要构建一个日历组件,其中包含从 JSON 响应映射的每个日期的时隙选择

jquery - 迷你图渲染速度慢并且浏览器挂起