Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In my dialog I have the design of pie chart that blocks the opening of other dialogs..I don't know why..I can post class code

What I have tried:

There is a block of messageBox and dialog of c++ of error, I can put PieChatWnd.cpp
<pre>// PieChartWnd.cpp : implementation file
//

#include "stdafx.h"
//#include "PieChart.h"
#include "PieChartWnd.h"
#include <math.h>
#include <limits>IMPLEMENT_DYNAMIC(CPieChartWnd, CWnd)

//CLASSE ADIBITA ALLA CREAZIONE DEL GRAFICO A TORTA

CPieChartWnd::CPieChartWnd():i_elementIndex(0), d_totalVal(0)
{
	lb_param.hght = 20;
	lb_param.wdth = 50;
	lb_param.xGapLeft =  200;
	lb_param.xGapRight = 20;
	lb_param.yGap = 10;
	lb_param.fStyle = FontStyleRegular;
	lb_param.sFont = "";

	lb_info.lbRctHght= 0;
	lb_info.lbIndex = 0;
	lb_info.lbVisibleRectHght = 1000;
	lb_info.lbTextColor = Color::Black;
	lb_info.lbValueRectColor = Color::Black;
	lb_info.sMaxLenString = "";

	bkg_params.cr_backgrnd = Color::White;
	bkg_params.cr_GradientD = Color::White;
	bkg_params.cr_GradientL = Color::White;
	bkg_params.s_ChartTittle = "";
	bkg_params.f_ColorGradD = 0;
	bkg_params.f_ColorGradL = 0;
	bkg_params.sFont = "";
	bkg_params.fStyle = FontStyleRegular;

	fl_startAngle = 0;
	fl_startAngleIncline = 0;
	fl_InclineAngle = 0;
	f_depth = .5;
	b_SaveFlag = FALSE;
	b_ShowPercentages = TRUE;
	pie_ChartStyle = ThreeDStyle;
	cr_HighlightColor = Color(255, 255, 255);
	s_saveImage = "";

}

CPieChartWnd::~CPieChartWnd()
{
	
	map<int, pie_chart_element*="">::iterator iter =  map_pChart.begin();
	for (; iter != map_pChart.end(); ++iter)
	{
		if(iter->second)
		{
			pie_chart_element* ele = iter->second;
			if(ele)
				delete ele;
		}
	}
	map_pChart.clear();
	Gdiplus::GdiplusShutdown(m_gdiplusToken);
}


BEGIN_MESSAGE_MAP(CPieChartWnd, CWnd)
	ON_WM_PAINT()
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_VSCROLL()
	ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()

// CPieChartWnd message handlers

void CPieChartWnd::OnPaint()
{
	CPaintDC dc(this);
	if (pie_ChartStyle == DoughnutStyle)
		DrawDoughnutStyle(&dc);
	if (pie_ChartStyle == TwoDStyle)
		Draw2DStyle(&dc);
	if (pie_ChartStyle == ThreeDStyle)
		Draw3DStyle(&dc);
		
	ManageTime(m_dPrimoValore,m_dSecondoValore);
}

int CPieChartWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	//This is to stop the flickering effects on scrollbar on resizing and painting.
	ModifyStyle(0, WS_CLIPCHILDREN);
	//Gdiplus::GdiplusStartupInput gdiplusStartupInput;
	//Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
	
	CRect rect;
	GetClientRect(rect);
	m_VscrollBar.Create(WS_CHILD | SBS_VERT,CRect(rect.right -40, rect.top + 10, rect.right -20, rect.bottom - 10), this, 1);
	return 0;
}

void CPieChartWnd::DrawDoughnutStyle(CDC* pDc)
{
	CRect rect;	
	CRect rectBnd;
	Bitmap* mBtmap = NULL;
	Graphics* graphics = NULL;
	float totalAngle = fl_startAngle;
	map<int, pie_chart_element*="">::iterator iter;
	Pen penOutLine(Color::White, 1); 
	
	GetBoundRect(rectBnd);
	GetClientRect(rect);
	
	//Create the memory bitmap to hold the drawing for the control area.	
	mBtmap = new Bitmap(rect.Width(), rect.Height());
	graphics = Graphics::FromImage(mBtmap);

	graphics->SetSmoothingMode(SmoothingModeHighQuality);
	
	DrawBackGround(graphics);
	LinearGradientBrush brBkgndInner(PointF((REAL)rect.left, (REAL) rect.top), 
								 PointF((REAL)rect.right, (REAL)rect.bottom), 
								 bkg_params.cr_GradientL, 
								 bkg_params.cr_GradientD);
	
	
	REAL rRatio = (REAL)rectBnd.Width() / 15;
	LinearGradientBrush crGradient(Point(rectBnd.right/2, rectBnd.top), 
								   Point(rectBnd.right/2, rectBnd.bottom), 
								   Color(0,0,0), 
								   Color(0,0,0));

	SolidBrush brHighLight(Color(100, cr_HighlightColor.GetR(), cr_HighlightColor.GetG(), cr_HighlightColor.GetB()));
	Pen pnHighLight(cr_HighlightColor);

	iter = map_pChart.begin();

	//Draw each pie item on the rectangle area.
	for(; iter != map_pChart.end(); ++iter)
	{
		pie_chart_element* ele = iter->second;
		if (ele){
			penOutLine.SetColor(CalculateGradientLight(ele->cr_Base, 160));
			crGradient.SetLinearColors(ele->cr_GradientL, ele->cr_GradientD);
			graphics->FillPie(&crGradient, 
							  RectF((REAL)rectBnd.left, (REAL)rectBnd.top, (REAL)rectBnd.Width(), (REAL)rectBnd.Height()), 
							  (REAL)totalAngle, 
							  (REAL)ele->f_angle);
			
			crGradient.SetLinearColors(ele->cr_GradientD, ele->cr_GradientL);

			graphics->FillPie(&crGradient, 
							  RectF(REAL(rectBnd.left + rRatio), 
							  REAL(rectBnd.top + rRatio), 
							  REAL(rectBnd.Width() - rRatio * 2), 
							  REAL(rectBnd.Height()- rRatio * 2)), 
							  (REAL)totalAngle, 
							  (REAL)ele->f_angle);

			graphics->DrawPie(&penOutLine, 
				              RectF((REAL)rectBnd.left, (REAL)rectBnd.top, (REAL)rectBnd.Width(), (REAL)rectBnd.Height()), 
							  (REAL)totalAngle, 
							  (REAL)ele->f_angle);

			if (ele->b_select){
					graphics->FillPie(&brHighLight, 
							  RectF((REAL)rectBnd.left, (REAL)rectBnd.top, (REAL)rectBnd.Width(), (REAL)rectBnd.Height()), 
							  (REAL)totalAngle, 
							  (REAL)ele->f_angle);
					graphics->DrawPie(&pnHighLight, 
							  RectF((REAL)rectBnd.left, (REAL)rectBnd.top, (REAL)rectBnd.Width(), (REAL)rectBnd.Height()), 
							  (REAL)totalAngle, 
							  (REAL)ele->f_angle);
			}	
			totalAngle += ele->f_angle;
		}
	}
	

	if (!map_pChart.empty())
	{
		graphics->FillEllipse(&brBkgndInner, RectF( REAL(rectBnd.CenterPoint().x - rRatio * 3),
									REAL(rectBnd.CenterPoint().y - rRatio * 3),
									rRatio * 6, rRatio * 6));
		penOutLine.SetBrush(&brBkgndInner);
		graphics->DrawEllipse(&penOutLine, RectF( REAL(rectBnd.CenterPoint().x - rRatio * 3),
							   REAL(rectBnd.CenterPoint().y - rRatio * 3),
							   rRatio * 6, rRatio * 6));
	}
	
	DrawLabels(graphics);
	
	if(b_ShowPercentages)
		DrawPiechartPecentages(graphics, fl_startAngle);

	//free the graphics object.
	delete graphics;
	graphics = NULL;

	Graphics gr(pDc->m_hDC);
	//Create the cached bitmap from the memory bitmap
	CachedBitmap* btmp = new CachedBitmap(mBtmap, &gr);

	SaveImageInternal(mBtmap);

	if (mBtmap)
	{
		delete mBtmap;
		mBtmap = NULL;
	}
	
	gr.DrawCachedBitmap(btmp, rect.left, rect.top);	
	
	if (btmp)
	{
		delete btmp;
		btmp = NULL;
	}
}

void CPieChartWnd::ManageTime(double dLoadTime,/* double dWorkTime,*/ double dUnloadTime/*, double dLostTime*/)
{
	CPieChartWnd::InsertItem(_T("Rendimento"), _T("Rendimento"), dLoadTime, Color(255, 255, 50, 50));
	//CPieChartWnd::InsertItem(_T("Tempo 2"), _T("Tempo 2"), dWorkTime, Color(255, 128, 255, 128));
	CPieChartWnd::InsertItem(_T("Restante"), _T("Restante"), dUnloadTime, Color(255, 166, 190, 255));//disegno il pezzo di torta
	//CPieChartWnd::InsertItem(_T("Lost time"),	_T("Lost time"),	dLostTime,	Color(255, 255, 255, 0));

	CPieChartWnd::SetInclineAngle(40);
	CPieChartWnd::SetDepth(40);
	CPieChartWnd::SetBackgrndColor(Color(255, 240, 240, 250));//colore di sfondo
	
	CRect rect;
	CPieChartWnd::GetClientRect(rect);
	
	Invalidate();
}

PIECHARTITEM CPieChartWnd::InsertItem(CString sElement, CString sLabel, double dValue, Color crColor)
{
	//Inserisco l'item riferito al pezzo di torta
	d_totalVal = 100.00;//valore totale
	
	if (sElement.Compare(L"") && !HasElement(sElement) && dValue > 0)
	{
		pie_chart_element* element = new pie_chart_element;
		element->s_element = sElement;
		element->s_label = sLabel;
		element->d_value = dValue;
		element->f_angle = 0;
		element->f_percentage = 0;
		element->i_ID = i_elementIndex;
		map_pChart[i_elementIndex] = element;
		i_elementIndex++;

		element->cr_Base = crColor;
		element->f_ColorGradD = GRADIENTD;
		element->f_ColorGradL = GRADIENTL;
	    element->cr_GradientL = CalculateGradientLight(crColor, GRADIENTL);
		element->cr_GradientD = CalculateGradientDark(crColor, GRADIENTD);
		element->b_select = FALSE;

		if (sLabel.GetLength() > lb_info.sMaxLenString.GetLength())
		{
			lb_info.sMaxLenString = sLabel;
		}

		UpdatePieChart();//togliere una percentuale dalla torta col colore;
		CalculateLabelRect();
		ResetItemScrollBar();
		Invalidate();
		return (PIECHARTITEM)element;
	}
   return NULL;
}

void CPieChartWnd::UpdatePieChart(void)
{
	map<int, pie_chart_element*="">::iterator iter = map_pChart.begin();
	
	iter = map_pChart.begin();
	for(; iter != map_pChart.end(); ++iter)
	{
		pie_chart_element* ele = iter->second;
		if (d_totalVal > 0)
		{
			ele->f_percentage = float(ele->d_value / d_totalVal);
			ele->f_angle = 360 * ele->f_percentage;
			ele->pie_3d_props.f_InclineAngle = ele->f_angle;			
		}
	}
}

Color CPieChartWnd::CalculateGradientLight(Color crBase, float fGradVal)
{

	BYTE r = crBase.GetR();
	BYTE g = crBase.GetG();
	BYTE b = crBase.GetB();

	float fact = 255.0f;
	float rGrad = (255 - r) / fact;
	float gGrad = (255 - g) / fact;
	float bGrad = (255 - b) / fact;
	
	r =  BYTE(min(r + rGrad * fGradVal, 255));
	b =  BYTE(min(b + bGrad * fGradVal, 255));
	g =  BYTE(min(g + gGrad * fGradVal, 255));

	return Color(r, g, b);
}

Color CPieChartWnd::CalculateGradientDark(Color crBase, float fGradVal)
{
	BYTE r = crBase.GetR();
	BYTE g = crBase.GetG();
	BYTE b = crBase.GetB();

	float fact = 255.0f;
	float rGrad = r / fact;
	float gGrad = g / fact;
	float bGrad = b / fact;

	r = BYTE(max(r - rGrad * fGradVal, 0));
	b = BYTE(max(b - bGrad * fGradVal, 0));
	g = BYTE(max(g - gGrad * fGradVal, 0));

	return Color(r, g, b);
}

BOOL CPieChartWnd::HasElement(CString sElement)
{
	map<int, pie_chart_element*="">::iterator iter;
	iter = map_pChart.begin();
	for(; iter != map_pChart.end(); ++iter)
	{
		pie_chart_element* ele = iter->second;
		if (!ele->s_element.Compare(sElement))
			return TRUE;
	}
	return FALSE;
}

int CPieChartWnd::GetElementID(CString sElement)
{
	map<int, pie_chart_element*="">::iterator iter;
	iter = map_pChart.begin();
	for(; iter != map_pChart.end(); ++iter)
	{
		pie_chart_element* ele = iter->second;
		if (!ele->s_element.Compare(sElement))
			return ele->i_ID;
	}
	return -1;
}

BOOL CPieChartWnd::RemoveItem(CString sElement)
{
	map<int, pie_chart_element*="">::iterator iter;
	
	iter = map_pChart.begin();
	for(; iter != map_pChart.end(); ++iter)
	{
		pie_chart_element* ele = iter->second;

		if (!ele->s_element.Compare(sElement))
		{
			d_totalVal -= ele->d_value;
			delete ele;
			ele = NULL;
			map_pChart.erase(iter);
			i_elementIndex--;
			ReArrangeElementMap();
			UpdatePieChart();
			CalculateLabelRect();
			GetVerticalScrollPos(m_VscrollBar.GetScrollPos(), false);
			ResetItemScrollBar();
			Invalidate();
			return TRUE;
		}
	}
	return FALSE;
}

BOOL CPieChartWnd::RemoveItem(int iElementID)
{
	map<int, pie_chart_element*="">::iterator iter;
	iter = map_pChart.find(iElementID);
	if (iter != map_pChart.end())
	{
		pie_chart_element* ele = iter->second;
		d_totalVal -= ele->d_value;
		delete ele;
		ele = NULL;
		map_pChart.erase(iter);
		i_elementIndex--;
		ReArrangeElementMap();
		UpdatePieChart();
		CalculateLabelRect();
		GetVerticalScrollPos(m_VscrollBar.GetScrollPos(), false);
		ResetItemScrollBar();
		Invalidate();
		return TRUE;
	}
	return FALSE;		
}

PIECHARTITEM CPieChartWnd::GetPieChartItem(CString sElement)
{
	map<int, pie_chart_element*="">::iterator iter;
	iter = map_pChart.begin();
	for(; iter != map_pChart.end(); ++iter)
	{
		pie_chart_element* ele = iter->second;
		if (!ele->s_element.Compare(sElement))
			return (PIECHARTITEM)ele;
	}
	return NULL;
}
Posted
Updated 5-Sep-22 2:25am
v2
Comments
OriginalGriff 2-Sep-22 4:52am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
Imagine this: you go for a drive in the country, but you have a problem with the car. You call the garage, say "it broke" and turn off your phone. How long will you be waiting before the garage arrives with the right bits and tools to fix the car given they don't know what make or model it is, who you are, what happened when it all went wrong, or even where you are?

That's what you've done here. So stop typing as little as possible and try explaining things to people who have no way to access your project!

Use the "Improve question" widget to edit your question and provide better information.
Richard MacCutchan 2-Sep-22 4:55am    
Neither do we since you provided no useful information.
Member 14594285 2-Sep-22 4:57am    
I use PieChartWnd class that blocks the opening of other windows..I don't have an other way to explain..
Richard MacCutchan 2-Sep-22 7:41am    
What do you mean when you say, "blocks the opening of other windows"? Where in this code are you trying to open another window?
Member 14594285 2-Sep-22 9:32am    
In my dialog there is a button where piechart is shown and then in my dialog there is a button that I press to open an other dialog

In the OnPaint() function, you call ManageTime(), which in turn calls Invalidate().

The invalidation causes WM_PAINT to be sent, and that calls OnPaint().

You get an infinite loop of self-sent messages. Better avoid this.

If the chart should really be renewed regularly, a timer would be a possible solution.
 
Share this answer
 
I solved calling the function ManageTime only I need and I comment out it inside OnPaint
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900