React less 实现纵横柱状图示例详解

目录
  • 引言
  • 主要设计来源
    • display 布局
    • display 布局
    • 动态位置使用绝对定位
    • style
    • JS

引言

之前的文章,咱们介绍过横向和竖向,具体的内容,请看

  • React + CSS 绘制横向柱状图
  • React + CSS 绘制竖状柱状图

这次,结合起来,横向和竖向,一起画

主要设计来源

三个部分

<ul className="vertical">
  <li className="vertical_li">100</li>
  <li className="vertical_li">75</li>
  <li className="vertical_li">50</li>
  <li className="vertical_li">25</li>
  <li className="vertical_li">0</li>
</ul>

display 布局

.vertical {
  height: 337px;
  font-size: 12px;
  font-weight: bold;
  color: #9eadca;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
<ul className="crosswise">
  <li>0</li>
  <li>25</li>
  <li>50</li>
  <li>75</li>
  <li>100</li>
</ul>

display 布局

.crosswise {
  width: 335px;
  font-size: 12px;
  font-weight: bold;
  color: #9eadca;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  margin-top: -31px;
  margin-left: -21px;
}
<div className="point_list">
  {list.map((item, index) => {
    return (
      <div
        className="point"
        style={{ top: `${100 - parseFloat(item.y)}%`, left: `${item.x}%` }}
        onMouseEnter={() => onMouseEnter(item, index)}
        onMouseLeave={() => onMouseLeave(index)}
        key={index}
      >
        {item.name}
      </div>
    )
  })}
</div>

动态位置使用绝对定位

.point_list {
  width: 308px;
  height: 308px;
  position: absolute;
  top: 0px;
  left: 0px;
}

具体的位置,是通过传入的参数来进行控制的。如果传入的参数不是具体的位置数值,前端也可以进行二次的计算。这里我就不演示了。之前的文章都有介绍,感兴趣的小伙伴可以去前两篇文章看一下。

style

ul,
li {
  list-style: none;
  padding: 0;
}
.parallel-comparison {
  height: 300px;
  padding-left: 35px;
  padding-top: 49px;
  padding-right: 29px;
  // height: 100%;
  .parallel_top {
    display: flex;
    height: 33px;
    align-items: center;
    .samll {
      display: inline-block;
      width: 4px;
      height: 24px;
      background-color: #085dff;
      border-radius: 3px;
    }
    .text {
      font-size: 24px;
      font-weight: 500;
      color: #085dff;
      line-height: 33px;
      margin-left: 8px;
      margin-right: 24px;
    }
    .history {
      padding: 5px 16px;
      background-color: #dce0e6;
      border-radius: 6px;
      color: #ffffff;
      font-size: 12px;
    }
  }
  .english {
    font-size: 18px;
    font-weight: 500;
    color: #ccd6e3;
  }
  .parallel_bottom {
    display: flex;
    margin-top: 48px;
    .left {
      height: 424px;
      box-shadow: 0px 0px 32px 0px rgba(0, 40, 96, 0.07);
      border-radius: 8px;
      padding-top: 15px;
      padding-left: 25px;
      h3 {
        font-size: 18px;
        font-weight: 400;
        color: #07132b;
      }
      .left_bottom {
        display: flex;
        width: 553px;
        .content {
          display: flex;
          flex-direction: column;
          .willingness {
            color: #9eadca;
            margin-left: 140px;
          }
          .gradual_change {
            display: flex;
            flex-direction: row;
            align-items: center;
            .box {
              width: 308px;
              height: 308px;
              background-color: #f2f6f6;
              margin-left: 27px;
              position: relative;
              .vertical {
                height: 337px;
                font-size: 12px;
                font-weight: bold;
                color: #9eadca;
                display: flex;
                flex-direction: column;
                margin-top: -8px;
                margin-left: -21px;
                justify-content: space-between;
              }
              .crosswise {
                width: 335px;
                font-size: 12px;
                font-weight: bold;
                color: #9eadca;
                display: flex;
                flex-direction: row;
                justify-content: space-between;
                margin-top: -31px;
                margin-left: -21px;
              }
              .point_list {
                width: 308px;
                height: 308px;
                position: absolute;
                top: 0px;
                left: 0px;
                .point {
                  position: absolute;
                  background-color: #ffffff;
                  text-align: center;
                  padding: 2px 5px;
                  font-size: 12px;
                  border-radius: 20px;
                  background: #e6eef4;
                  box-shadow: 20px 20px 60px #c4cacf, -20px -20px 60px #ffffff;
                }
              }
            }
            .good_value {
              display: inline-block;
              width: 15px;
              writing-mode: vertical-lr;
              color: #9eadca;
              font-size: 14px;
              margin-left: 12px;
              margin-right: 4px;
            }
          }
        }
      }
    }
  }
}

JS

import React, { useState, useEffect } from 'react';
import ReactDom from 'react-dom';
const ParallelComparison = ({ gradualChangeDataList }) => {
  const [list, setList] = useState(gradualChangeDataList)
  useEffect(() => {
    const _list = list
      .map((item) => {
        return {
          ...item,
          sum: parseFloat(item.x) + parseFloat(item.y),
          isHover: false
        }
      })
      .sort((a, b) => b.sum - a.sum)
  }, [gradualChangeDataList])
  return (
    <div className="parallel-comparison">
      <div className="parallel_bottom">
        <div className="left">
          <div className="left_bottom">
            <div className="content">
              <div className="gradual_change">
                <div className="box">
                  <ul className="vertical">
                    <li className="vertical_li">100</li>
                    <li className="vertical_li">75</li>
                    <li className="vertical_li">50</li>
                    <li className="vertical_li">25</li>
                    <li className="vertical_li">0</li>
                  </ul>
                  <ul className="crosswise">
                    <li>0</li>
                    <li>25</li>
                    <li>50</li>
                    <li>75</li>
                    <li>100</li>
                  </ul>
                  <div className="point_list">
                    {list.map((item, index) => {
                      return (
                        <div
                          className="point"
                          style={{ top: `${100 - parseFloat(item.y)}%`, left: `${item.x}%` }}
                          key={index}
                        >
                          {item.name}
                        </div>
                      )
                    })}
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  )
}
const Test = function () {
  const _arr = new Array()
for (let i = 0; i < 5; i++) {
  _arr.push({
    id: i,
    x: (Math.random() * 100).toFixed(2),
    y: (Math.random() * 100).toFixed(2),
    name: '碧螺春',
  })
}
  return (
    <ParallelComparison gradualChangeDataList={_arr} />
  );
};
ReactDom.render(<Test />, document.getElementById('app'));
      

以上就是React less 实现纵横柱状图示例详解的详细内容,更多关于React less纵横柱状图的资料请关注我们其它相关文章!

(0)

相关推荐

  • 详解在create-react-app使用less与antd按需加载

    使用antd按需加载 使用react-app-rewired对 create-react-app 的默认配置进行自定义 1.yarn add react-app-rewired --dev /* package.json */ "scripts": { - "start": "react-scripts start", + "start": "react-app-rewired start", - &quo

  • 详解在React项目中安装并使用Less(用法总结)

    less的安装配置 安装react-app-rewired,react-app-rewire-less,babel-plugin-import插件 执行命令: npm install react-app-rewired --save-dev npm install react-app-rewire-less --save-dev npm install babel-plugin-import --save-dev 配置package.json文件 找到scripts属性,修改start的值为re

  • create-react-app中添加less支持的实现

    前言 使用 create-react-app 脚手架创建项目后,默认是不支持 less 的.所以我们需要手动添加. 第一步 暴露webpack配置文件 使用 create-react-app 创建的项目,默认情况下是看不到 webpack 相关的配置文件,我们需要给它暴露出来,使用下面命令即可: npm run eject 运行之后,我们发现多了一个config文件夹 这样就可以修改 webpack 相关配置了. 第二步 添加less 在项目根目录 使用 npm 或者 yarn 来安装 less

  • 在Create React App中启用Sass和Less的方法示例

    关于创建 create-react-app 项目请查看:create-react-app 的安装与创建 . 关于在 less 和 sass 如何在 create-react-app 启用 CSS Modules,请查看我的上一篇文章: 在 Create React App 中使用 CSS Modules . 启用 Sass 语法编写 CSS create-react-app 脚手架中已经添加了 sass-loader 的支持,所以只需要安装 node-sass 插件即可 安装 node-sass

  • react脚手架如何配置less和ant按需加载的方法步骤

    前言 create-react-app是由React官方提供并推荐使用构建新的React单页面应用程序的最佳方式,其构建的项目默认是不支持less的,需要我们手动集成 一.react脚手架搭建 1.先全局安装create-react-app(提前需要安装node) npm install -g create-react-app 2.然后通过create-react-app创建项目my-app create-react-app my-app 3.最后通过cd进入项目文件夹并启动 cd my-app

  • react如何添加less环境配置

    目录 1.安装less 2.找到node_modules文件夹里react-scripts/config/webpack.config.js 3.然后往下翻代码到下图 4.在src目录下创建一个less文件,里面写样式 5.引入 6.如果启动报错如下,就说明less-loader版本太高 7.解决方案,修改less-loader版本,大概6.0.0就好了 1.安装less npm install less-loader less --save-dev 2.找到node_modules文件夹里re

  • React less 实现纵横柱状图示例详解

    目录 引言 主要设计来源 display 布局 display 布局 动态位置使用绝对定位 style JS 引言 之前的文章,咱们介绍过横向和竖向,具体的内容,请看 React + CSS 绘制横向柱状图 React + CSS 绘制竖状柱状图 这次,结合起来,横向和竖向,一起画 主要设计来源 三个部分 <ul className="vertical"> <li className="vertical_li">100</li>

  • react redux及redux持久化示例详解

    目录 一.react-redux 二.redux持久化 一.react-redux react-redux依赖于redux工作. 运行安装命令:npm i react-redux: 使用: 将Provider套在入口组件处,并且将自己的store传进去: import FilmRouter from './FilmRouter/index' import {Provider} from 'react-redux' import store from './FilmRouter/views/red

  • react编写可编辑标题示例详解

    目录 需求 初始需求 方案设计 方案一 span + contentEditable 思路 代码如下 在这个方案中遇到的问题 存在的问题 方案二 直接用input处理展示和编辑 踩到的坑 需求 因为自己换工作到了新公司,上周入职,以前没有使用过react框架,虽然前面有学习过react,但是并没有实践经验 这个需求最终的效果是和石墨标题修改实现一样的效果 初始需求 文案支持可编辑 用户点击位置即光标定位处 超过50字读的时候,超出部分进行截断 当用户把所有内容删除时,失去焦点时文案设置为 “无文

  • React特征Form 单向数据流示例详解

    目录 引言 集中状态管理 双向数据流 那为何不选择双向数据流 小结 引言 今天将之前的内容做个系统整理,结合 React Form 案例, 来了解下为何React推荐单向数据流,如果采用双向管理,可能的问题 (关于React Form案例,可参考相关文章 - 学习React的特征(二) - React Form 集中状态管理 首先来看之前的React Form, 若采用单向数据流 import * as React from 'react'; const Useremail = props =>

  • react后台系统最佳实践示例详解

    目录 一.中后台系统的技术栈选型 1. 要做什么 2. 要求 3. 技术栈怎么选 二.hooks时代状态管理库的选型 context redux recoil zustand MobX 三.hooks的使用问题与解决方案 总结 一.中后台系统的技术栈选型 本文主要讲三块内容:中后台系统的技术栈选型.hooks时代状态管理库的选型以及hooks的使用问题与解决方案. 1. 要做什么 我们的目标是搭建一个适用于公司内部中后台系统的前端项目最佳实践. 2. 要求 由于业务需求比较多,一名开发人员需要负

  • React特征学习Form数据管理示例详解

    目录 Form数据管理 重置Form状态 form验证 小结 Form数据管理 有时会遇到多个位置需要用户输入的情况,若每个状态都配置state或handler会很繁琐,可以尝试下面的方法 import * as React from 'react'; const LoginForm = () => { // 将多个状态合并为对象 const [state, setState] = React.useState({ email: '', password: '', }); // 通过单个hand

  • React学习笔记之列表渲染示例详解

    前言 本文主要给大家介绍了关于React列表渲染的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧. 示例详解: 列表渲染也很简单,利用map方法返回一个新的渲染列表即可,例如: const numbers = [1, 2, 3, 4, 5]; const listItems = numbers.map((number) => <li>{number}</li> ); ReactDOM.render( <ul>{listItems}<

  • React 路由使用示例详解

    目录 Router 简单路由 嵌套路由 未匹配路由 路由传参数 索引路由 活动链接 搜索参数 自定义行为 useNavigate 参考资料 Router react-router-dom是一个处理页面跳转的三方库,在使用之前需要先安装到我们的项目中: # npm npm install react-router-dom@6 #yarn yarn add react-router-dom@6 简单路由 使用路由时需要为组件指定一个路由的path,最终会以path为基础,进行页面的跳转.具体使用先看

  • React路由拦截模式及withRouter示例详解

    目录 一.路由拦截 二.路由模式 三.withRouter 一.路由拦截 在前面两篇 路由博客基础上,我们将ReactRouter.js的我的profile路由设置成路由拦截的: <Route path="/profile" render={() => isAuth() ? <Profile/> : <Redirect to="/login"></Redirect> }></Route> 新建Logi

  • React Native 中实现确认码组件示例详解

    目录 正文 实现原理 开源方案 正文 确认码控件也是一个较为常见的组件了,乍一看,貌似较难实现,但实则主要是障眼法. 实现原理 上图 CodeInput 组件的 UI 结构如下: <View style={[styles.container]}> <TextInput autoFocus={true} /> <View style={[styles.cover, StyleSheet.absoluteFillObject]} pointerEvents="none&

随机推荐