Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Pie chart #204

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/cpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ add_example(bubblechart bubblechart.cpp cpu CXX11)
add_example(field field.cpp cpu)
add_example(fractal fractal.cpp cpu)
add_example(histogram histogram.cpp cpu)
add_example(pie pie.cpp cpu)
add_example(plot3 plot3.cpp cpu)
add_example(plotting plotting.cpp cpu)
add_example(stream stream.cpp cpu)
Expand Down
81 changes: 81 additions & 0 deletions examples/cpu/pie.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*******************************************************
* Copyright (c) 2015-2019, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/

#include <forge.h>
#define USE_FORGE_CPU_COPY_HELPERS
#include <ComputeCopy.h>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <vector>

const unsigned DIMX = 1000;
const unsigned DIMY = 800;
const unsigned NSECTORS = 10;

std::vector<float> generateSectors(unsigned count = NSECTORS) {
std::vector<float> result;
float prefixSum = 0;
for (; count > 0; --count) {
result.push_back(prefixSum);
result.push_back(std::rand() & 0xffff);
prefixSum += result.back();
}
return result;
}

std::vector<float> generateColors(unsigned count = NSECTORS) {
std::vector<float> result;
for (; count > 0; --count) {
for (int channel = 0; channel < 3; ++channel)
result.push_back(std::rand() / (float)RAND_MAX);
}
return result;
}

int main(int argc, char* argv[]) {
/*
* First Forge call should be a window creation call
* so that necessary OpenGL context is created for any
* other forge::* object to be created successfully
*/
forge::Window wnd(DIMX, DIMY, "Pie Demo");
wnd.makeCurrent();

forge::Chart chart(FG_CHART_2D);

/*
* Create pie object specifying number of bins
*/
forge::Pie pie = chart.pie(NSECTORS, forge::f32);

GfxHandle* handles[2];
createGLBuffer(&handles[0], pie.vertices(), FORGE_VERTEX_BUFFER);
createGLBuffer(&handles[1], pie.colors(), FORGE_VERTEX_BUFFER);

std::vector<float> pieArray = generateSectors();
std::vector<float> colArray = generateColors();

/* set the axes limits to minimum and maximum values of data */
float valueRange =
pieArray[pieArray.size() - 2] + pieArray[pieArray.size() - 1];
chart.setAxesLimits(0, valueRange, 0, valueRange);

copyToGLBuffer(handles[0], (ComputeResourceHandle)pieArray.data(),
pie.verticesSize());
copyToGLBuffer(handles[1], (ComputeResourceHandle)colArray.data(),
pie.colorsSize());

do { wnd.draw(chart); } while (!wnd.close());

releaseGLBuffer(handles[0]);
releaseGLBuffer(handles[1]);

return 0;
}
62 changes: 60 additions & 2 deletions include/fg/chart.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
#pragma once

#include <fg/defines.h>
#include <fg/histogram.h>
#include <fg/image.h>
#include <fg/pie.h>
#include <fg/plot.h>
#include <fg/surface.h>
#include <fg/vector_field.h>
#include <fg/histogram.h>


#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -148,6 +148,16 @@ FGAPI fg_err fg_append_image_to_chart(fg_chart pChart, fg_image pImage);
*/
FGAPI fg_err fg_append_histogram_to_chart(fg_chart pChart, fg_histogram pHistogram);

/**
Add an existing pie object to chart

\param[in] pChart is the chart to which pie has to be added
\param[in] pPie is the pie to be added to the chart

\return \ref fg_err error code
*/
FGAPI fg_err fg_append_pie_to_chart(fg_chart pChart, fg_pie pPie);

/**
Add an existing plot object to chart

Expand Down Expand Up @@ -211,6 +221,21 @@ FGAPI fg_err fg_add_image_to_chart(fg_image* pImage, fg_chart pHandle,
FGAPI fg_err fg_add_histogram_to_chart(fg_histogram* pHistogram, fg_chart pHandle,
const unsigned pNBins, const fg_dtype pType);

/**
Create and add a Pie object to the current chart

\param[out] pPie is the handle of the pie object created
\param[in] pHandle is chart handle
\param[in] pNSectors is number of sectors the data is sorted out
\param[in] pType takes one of the values of \ref fg_dtype that indicates
the integral data type of pie data

\return \ref fg_err error code
*/
FGAPI fg_err fg_add_pie_to_chart(fg_pie *pPie, fg_chart pHandle,
const unsigned pNSectors,
const fg_dtype pType);

/**
Create and add an Plot object to the current chart

Expand Down Expand Up @@ -283,6 +308,16 @@ Remove a Histogram object from the current chart
FGAPI fg_err fg_remove_histogram_from_chart(fg_chart pHandle,
fg_histogram pHistogram);

/**
Remove a Pie object from the current chart

\param[in] pHandle is chart handle
\param[in] pPie is the handle of the pie object to remove

\return \ref fg_err error code
*/
FGAPI fg_err fg_remove_pie_from_chart(fg_chart pHandle, fg_pie pPie);

/**
Remove a Plot object from the current chart

Expand Down Expand Up @@ -459,6 +494,13 @@ class Chart {
*/
FGAPI void add(const Histogram& pHistogram);

/**
Add an existing Pie object to the current chart

\param[in] pPie is the Pie to render on the chart
*/
FGAPI void add(const Pie &pPie);

/**
Add an existing Plot object to the current chart

Expand Down Expand Up @@ -494,6 +536,13 @@ class Chart {
*/
FGAPI void remove(const Histogram &pHistogram);

/**
Remove an existing Pie object to the current chart

\param[in] pPie is the Pie to remove from the chart
*/
FGAPI void remove(const Pie &pPie);

/**
Remove an existing Plot object to the current chart

Expand Down Expand Up @@ -537,6 +586,15 @@ class Chart {
*/
FGAPI Histogram histogram(const unsigned pNBins, const dtype pDataType);

/**
Create and add a Pie object to the current chart

\param[in] pNSectors is number of sectors the data is sorted out
\param[in] pDataType takes one of the values of \ref dtype that
indicates the integral data type of pie data
*/
FGAPI Pie pie(const unsigned pNSectors, const dtype pDataType);

/**
Create and add an Plot object to the current chart

Expand Down
3 changes: 3 additions & 0 deletions include/fg/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ typedef void* fg_image;
/// \brief Histogram handle
typedef void* fg_histogram;

/// \brief Pie handle
typedef void *fg_pie;

/// \brief Plot handle
typedef void* fg_plot;

Expand Down
4 changes: 2 additions & 2 deletions include/fg/histogram.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ FGAPI fg_err fg_release_histogram(fg_histogram pHistogram);
This is global alpha value for the histogram rendering that takes
effect if individual bar alphas are not set by calling the following
member functions
- Histogram::alphas()
- Histogram::alphasSize()
- fg_get_histogram_alpha_buffer
- fg_get_histogram_alpha_buffer_size

\param[in] pHistogram is the histogram handle
\param[in] pRed is Red component in range [0, 1]
Expand Down
Loading