折线图绘制代码:

#include<iostream>

//旧版本 固定管线
#include<Windows.h>
#include <GL/glut.h>
//新版本 可编程管线
/*#define GLEW_STATIC
#include <GL/glew.h>
#include<GLFW\glfw3.h>
*/ using namespace std; GLsizei winWidth = , winHeight = ;
GLint xRaster = , yRaster = ; GLubyte label[] = { 'J','a','n', 'F','e','b', 'M','a','r', 'A','p','r',
'M','a','y', 'J','u','n', 'J','u','l', 'A','u','g',
'S','e','p', 'O','c','t', 'N','o','v', 'D','e','c' }; GLint dataValue[] = { ,,,,,,,,,,, }; void init()
{
//窗口背景为白色
glClearColor(, , , );
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 610.0, 0.0, 500.0);
} void lineGraph()
{
GLint month, k;
GLint x = ; glClear(GL_COLOR_BUFFER_BIT);
//蓝色
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINE_STRIP); //画出折线段
for (k = ; k < ; k++) {
glVertex2i(x + k * , dataValue[k]);
}
glEnd(); //红色
glColor3f(1.0, 0.0, 0.0); //标注各点
for (k = ; k < ; k++) {
glRasterPos2i(xRaster + k * , dataValue[k] - );
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, '*');
} //黑色
glColor3f(0.0, 0.0, 0.0); //横坐标说明
xRaster = ;
for (month = ; month < ; month++) {
glRasterPos2i(xRaster, yRaster);
for (k = * month;k < * month + ;k++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, label[k]);
xRaster += ;
}
}
glFlush();
} void winReshapeFcn(GLint newWidth, GLint newHeight)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(, GLdouble(newWidth), , GLdouble(newHeight));
glClear(GL_COLOR_BUFFER_BIT);
} int main(int argc, char* argv[])
{ //对GLUT进行初始化,并处理所有的命令行参数
glutInit(&argc, argv);
//指定使用RGBA模式还是颜色索引模式,还可指定使用单缓冲还是双缓冲窗口
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
//指定了窗口左上角在屏幕上的位置
glutInitWindowPosition(, );
//制定了窗口大小(以像素为单位)
glutInitWindowSize(winWidth, winHeight);
//创建支持opengl渲染的窗口,在调用glutMainLoop函数前,窗口没有显示
glutCreateWindow("折线图");
init();
//显示回调函数
//每当GLUT确定一个窗口的内容需要重新显示时,通过glutDisplayFunc()注册的那个回调函数就会被执行
glutDisplayFunc(lineGraph);
glutReshapeFunc(winReshapeFcn);
//启动程序,所有窗口这时显示
glutMainLoop(); system("pause");
return ; }

运行结果:

柱状图代码:

void barChart()
{
GLint month, k; glClear(GL_COLOR_BUFFER_BIT); //红色
glColor3f(1.0, 0.0, 0.0);
for (k = ; k < ; k++) {
glRecti( + k * , , + k * , dataValue[k]);
} //黑色
glColor3f(0.0, 0.0, 0.0); //横坐标说明
xRaster = ;
for (month = ; month < ; month++) {
glRasterPos2i(xRaster, yRaster);
for (k = * month;k < * month + ;k++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, label[k]);
xRaster += ;
}
}
glFlush();
}

运行结果:

饼图代码:

#include<iostream>
#include <math.h>
//旧版本 固定管线
#include<Windows.h>
#include <GL/glut.h>
//新版本 可编程管线
/*#define GLEW_STATIC
#include <GL/glew.h>
#include<GLFW\glfw3.h>
*/ using namespace std; const GLdouble twoPi = 6.283185; class scrPt {
public:
scrPt() {
x = y = ;
}
GLint x, y;
void setCoords(GLint xCoordValue, GLint yCorrdValue) {
x = xCoordValue;
y = yCorrdValue;
}
GLint getx() const {
return x;
}
GLint gety() const {
return y;
}
void incrementx() {
x++;
}
void incrementy() {
y--;
} }; GLsizei winWidth = , winHeight = ; void init()
{
//窗口背景为白色
glClearColor(, , , );
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
} void setPixel(GLint xCoord, GLint yCoord)
{
glBegin(GL_POINTS);
glVertex2i(xCoord, yCoord);
glEnd();
} void circlePlotPoints(GLint xc, GLint yc, scrPt circPt)
{
setPixel(xc + circPt.getx(), yc + circPt.gety());
setPixel(xc - circPt.getx(), yc + circPt.gety());
setPixel(xc + circPt.getx(), yc - circPt.gety());
setPixel(xc - circPt.getx(), yc - circPt.gety());
setPixel(xc + circPt.gety(), yc + circPt.getx());
setPixel(xc - circPt.gety(), yc + circPt.getx());
setPixel(xc + circPt.gety(), yc - circPt.getx());
setPixel(xc - circPt.gety(), yc - circPt.getx());
} //中心画圆算法
void circleMidpoint(GLint xc, GLint yc, GLint radius)
{
scrPt circPt;
GLint p = - radius;//中点参数初值
circPt.setCoords(, radius);
circlePlotPoints(xc, yc, circPt);
while (circPt.getx() < circPt.gety()) {
circPt.incrementx();
if (p < )
p += * circPt.getx() + ;
else {
circPt.incrementy();
p += * (circPt.getx() - circPt.gety()) + ;
}
circlePlotPoints(xc, yc, circPt);
}
} void pieChart()
{
scrPt circCtr, piePt;
GLint radius = winWidth / ;
GLdouble sliceAngle, previousSliceAngle = 0.0; GLint k, nSlices = ; GLfloat dataValues[] = { 10.0,7.0,13.0,5.0,13.0,14.0,
3.0,16.0,5.0,3.0,17.0,8.0 };
GLfloat dataSum = 0.0; circCtr.x = winWidth / ;
circCtr.y = winHeight / ;
circleMidpoint(circCtr.x,circCtr.y, radius); for (k = ;k < nSlices;k++) {
dataSum += dataValues[k];
} for (k = ; k < ; k++) {
sliceAngle = twoPi * dataValues[k] / dataSum + previousSliceAngle;
piePt.x = circCtr.x + radius * cos(sliceAngle);
piePt.y = circCtr.y + radius * sin(sliceAngle);
glBegin(GL_LINES);
glVertex2i(circCtr.x, circCtr.y);
glVertex2i(piePt.x, piePt.y);
glEnd();
previousSliceAngle = sliceAngle;
} } void displayFcn()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
pieChart();
glFlush();
} void winReshapeFcn(GLint newWidth, GLint newHeight)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, GLdouble(newWidth), 0.0, GLdouble(newHeight));
glClear(GL_COLOR_BUFFER_BIT); winWidth = newWidth;
winHeight = newHeight;
} int main(int argc, char* argv[])
{ //对GLUT进行初始化,并处理所有的命令行参数
glutInit(&argc, argv);
//指定使用RGBA模式还是颜色索引模式,还可指定使用单缓冲还是双缓冲窗口
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
//指定了窗口左上角在屏幕上的位置
glutInitWindowPosition(, );
//制定了窗口大小(以像素为单位)
glutInitWindowSize(winWidth, winHeight);
//创建支持opengl渲染的窗口,在调用glutMainLoop函数前,窗口没有显示
glutCreateWindow("Pie");
init();
//显示回调函数
//每当GLUT确定一个窗口的内容需要重新显示时,通过glutDisplayFunc()注册的那个回调函数就会被执行
glutDisplayFunc(displayFcn);
glutReshapeFunc(winReshapeFcn);
//启动程序,所有窗口这时显示
glutMainLoop(); system("pause");
return ; }

运行结果:

OpenGL——折线图柱状图饼图绘制的更多相关文章

  1. 百度推出的echarts,制表折线图柱状图饼图等的超级工具(转)

    一.简介: 1.绘制数据图表,有了它,想要网页上绘制个折线图.柱状图,从此easy. 2.使用这个百度的echarts.js插件,是通过把图片绘制在canvas上在显示在页面上. 官网对echarts ...

  2. G2 基本使用 折线图 柱状图 饼图 基本配置

    G2的基本使用 1.浏览器引入  <!-- 引入在线资源 --> <script src="https://gw.alipayobjects.com/os/lib/antv ...

  3. JavaScript数据可视化编程学习(一)Flotr2,包含简单的,柱状图,折线图,饼图,散点图

    一.基础柱状图 二.基础的折线图 三.基础的饼图 四.基础的散点图 一.基础柱状图 如果你还没有想好你的数据用什么类型的图表来展示你的数据,你应该首先考虑是否可以做成柱状图.柱状图可以表示数据的变化过 ...

  4. 安卓图表引擎AChartEngine(三) - 示例源码折线图、饼图和柱状图

    折线图: package org.achartengine.chartdemo.demo.chart; import java.util.ArrayList; import java.util.Lis ...

  5. HighCharts之2D柱状图、折线图和饼图的组合图

    HighCharts之2D柱状图.折线图和饼图的组合图 1.实例源码 ColumnLinePie.html: <!DOCTYPE html> <html> <head&g ...

  6. 利用pandas读取Excel表格,用matplotlib.pyplot绘制直方图、折线图、饼图

    利用pandas读取Excel表格,用matplotlib.pyplot绘制直方图.折线图.饼图 数据: 折线图代码: import  pandas  as pdimport  matplotlib. ...

  7. 数据可视化(Echart) :柱状图、折线图、饼图等六种基本图表的特点及适用场合

    数据可视化(Echart) 柱状图.折线图.饼图等六种基本图表的特点及适用场合 参考网址 效果图 源码 <!DOCTYPE html> <html> <head> ...

  8. v-charts 绘制柱状图、条形图、水球图、雷达图、折线图+柱状图,附官网地址

    v-charts 官网地址:https://v-charts.js.org/#/ 柱状图: <template> <ve-histogram :data="chartDat ...

  9. ChartControl 折线图 柱状图

    添加折线图(柱状图) 拖动ChartControl到Form上 在Series Collection中添加Line(或Bar) DevExpress.XtraCharts.Series series1 ...

随机推荐

  1. 慎用ArrayList的contains方法,使用HashSet的contains方法代替

    在启动一个应用的时候,发现其中有一处数据加载要数分钟,刚开始以为是需要load的数据比较多的缘故,查了一下数据库有6条左右,但是单独写了一个数据读取的方法,将这6万多条全部读过来,却只需要不到10秒钟 ...

  2. VS2010链接TFS遇见错误:TF204017,没有访问工作区域,需要一个或者多个必须权限

    最近刚刚搭建好服务器,然后准备将VSS源代码迁移到TFS源代码管理服务器上面.在我本机先用的服务器帐号来上传初始化源代码数据库,然后我又用自己的帐号进行迁出代码的时候发生的异常. 造成上述错误,主要是 ...

  3. webservice接口与HTTP接口学习笔记

    一.webservice 的概念 Web 是使应用程序可以与平台和编程语言无关的方式进行相互通信的一项技术.Web 服务是一个软件接口,它描述了一组可以在网络上通过标准化的 XML 消息传递访问的操作 ...

  4. 如何在IntelliJ IDEA中使用Git .ignore插件忽略不必要提交的文件

    参考  https://blog.csdn.net/qq_34590097/article/details/56284935 最近初学Git,而且在使用的IDE是IntelliJ IDEA,发现IDE ...

  5. Exception的ToString()方法究竟返回的是什么

    最近项目上线后遇到exception没有堆栈信息.所以跟踪一下 源码,其中主要的code如下: // Returns the stack trace as a string. If no stack ...

  6. tmux分屏幕

    1. tmux  a  -t  fly 连接上tmux 2. 左右分屏幕,ctrl+a ,再按% 上下分屏: ctrl+a, 再按“ 切换屏幕: ctrl+a, 再按o 关闭终端: ctrl+a, 再 ...

  7. Axure RP for Mac(网站交互式原型设计工具)破解版安装

    1.软件简介    Axure RP 是 macOS 系统上一款最知名和最强大的原型设计工具,增加了大量新的特性,如应用多个动画,并同一时间运行一个小部件,如褪色,同时移动等,而且具有全新的图标和界面 ...

  8. spring-mybatis代码生成插件,与实例展示

    前段时间看了张开涛写的代码生成插件,感觉思路很好,通过连接库然后获取数据库表信息,然后用户在界面中勾选要映射的策略,映射的字段,然后可以自动生成业务代码. 基于开涛的思路,自己写了一个简易插件,去掉了 ...

  9. WRI$_ADV_OBJECTS表过大,导致PDB的SYSAUX表空间不足

    现象监控发现sysaux表空间使用不断增加,导致表空间不足 查看过程 查看版本: SQL> select * from v$version; BANNER CON_ID ------------ ...

  10. [剑指Offer]5.二维数组中的查找

    题目 在一个二维数组中,每一行都依照从左到右递增的顺序排序,每一列都依照从上到下递增的顺序排序.请完毕一个函数,输入这种一个二维数组和一个整数.推断数组中是否含有该整数. 思路 [算法系列之三十三]杨 ...