/// <summary> /// Whether to show serie and legend. /// </summary> /// <param name="serieIndex">the index of serie</param> /// <param name="active">Active or not</param> public virtual void SetActive(int serieIndex, bool active) { m_Series.SetActive(serieIndex, active); var serie = m_Series.GetSerie(serieIndex); if (serie != null && !string.IsNullOrEmpty(serie.name)) { var bgColor1 = active ? m_ThemeInfo.GetColor(serie.index) : m_ThemeInfo.legendUnableColor; m_Legend.UpdateButtonColor(serie.name, bgColor1); } }
private void InitLegend() { m_Legend.OnChanged(); ChartHelper.HideAllObject(transform, s_LegendObjectName); TextAnchor anchor = m_Legend.location.textAnchor; Vector2 anchorMin = m_Legend.location.anchorMin; Vector2 anchorMax = m_Legend.location.anchorMax; Vector2 pivot = m_Legend.location.pivot; var legendObject = ChartHelper.AddObject(s_LegendObjectName, transform, anchorMin, anchorMax, pivot, new Vector2(chartWidth, chartHeight)); legendObject.transform.localPosition = m_Legend.location.GetPosition(chartWidth, chartHeight); ChartHelper.HideAllObject(legendObject, s_LegendObjectName); for (int i = 0; i < m_Legend.data.Count; i++) { Button btn = ChartHelper.AddButtonObject(s_LegendObjectName + "_" + i, legendObject.transform, m_ThemeInfo.font, m_Legend.itemFontSize, m_ThemeInfo.legendTextColor, anchor, anchorMin, anchorMax, pivot, new Vector2(m_Legend.itemWidth, m_Legend.itemHeight)); m_Legend.SetButton(i, btn); m_Legend.SetActive(i, IsActive(i)); m_Legend.UpdateButtonColor(i, m_ThemeInfo.GetColor(i), m_ThemeInfo.legendUnableColor); btn.GetComponentInChildren <Text>().text = m_Legend.data[i]; ChartHelper.AddEventListener(btn.gameObject, EventTriggerType.PointerDown, (data) => { int count = (data as PointerEventData).clickCount; int index = int.Parse(data.selectedObject.name.Split('_')[1]); SetActive(index, !m_Legend.IsActive(index)); m_Legend.UpdateButtonColor(index, m_ThemeInfo.GetColor(index), m_ThemeInfo.legendUnableColor); OnYMaxValueChanged(); OnLegendButtonClicked(); RefreshChart(); }); } }
private void InitLegend() { m_Legend.OnChanged(); TextAnchor anchor = m_Legend.location.textAnchor; Vector2 anchorMin = m_Legend.location.anchorMin; Vector2 anchorMax = m_Legend.location.anchorMax; Vector2 pivot = m_Legend.location.pivot; var legendObject = ChartHelper.AddObject(s_LegendObjectName, transform, anchorMin, anchorMax, pivot, new Vector2(chartWidth, chartHeight)); legendObject.transform.localPosition = m_Legend.location.GetPosition(chartWidth, chartHeight); ChartHelper.DestoryAllChilds(legendObject.transform); if (!m_Legend.show) { return; } var serieNameList = m_Series.GetSerieNameList(); List <string> datas; if (m_Legend.data.Count > 0) { datas = new List <string>(); for (int i = 0; i < m_Legend.data.Count; i++) { var category = m_Legend.data[i]; if (serieNameList.Contains(category)) { datas.Add(category); } } } else { datas = serieNameList; } m_Legend.RemoveButton(); for (int i = 0; i < datas.Count; i++) { string legendName = datas[i]; Button btn = ChartHelper.AddButtonObject(s_LegendObjectName + "_" + i + "_" + legendName, legendObject.transform, m_ThemeInfo.font, m_Legend.itemFontSize, m_ThemeInfo.legendTextColor, anchor, anchorMin, anchorMax, pivot, new Vector2(m_Legend.itemWidth, m_Legend.itemHeight)); var bgColor = IsActiveByLegend(legendName) ? m_ThemeInfo.GetColor(i) : m_ThemeInfo.legendUnableColor; m_Legend.SetButton(legendName, btn, datas.Count); m_Legend.UpdateButtonColor(legendName, bgColor); btn.GetComponentInChildren <Text>().text = legendName; ChartHelper.ClearEventListener(btn.gameObject); ChartHelper.AddEventListener(btn.gameObject, EventTriggerType.PointerDown, (data) => { if (data.selectedObject == null || m_Legend.selectedMode == Legend.SelectedMode.None) { return; } var temp = data.selectedObject.name.Split('_'); string selectedName = temp[2]; int clickedIndex = int.Parse(temp[1]); if (m_Legend.selectedMode == Legend.SelectedMode.Multiple) { OnLegendButtonClick(clickedIndex, selectedName, !IsActiveByLegend(selectedName)); } else { var btnList = m_Legend.buttonList.Values.ToArray(); for (int n = 0; n < btnList.Length; n++) { temp = btnList[n].name.Split('_'); selectedName = temp[2]; var index = int.Parse(temp[1]); OnLegendButtonClick(n, selectedName, index == clickedIndex ? true : false); } } }); ChartHelper.AddEventListener(btn.gameObject, EventTriggerType.PointerEnter, (data) => { if (btn == null) { return; } var temp = btn.name.Split('_'); string selectedName = temp[2]; int index = int.Parse(temp[1]); OnLegendButtonEnter(index, selectedName); }); ChartHelper.AddEventListener(btn.gameObject, EventTriggerType.PointerExit, (data) => { if (btn == null) { return; } var temp = btn.name.Split('_'); string selectedName = temp[2]; int index = int.Parse(temp[1]); OnLegendButtonExit(index, selectedName); }); } if (m_Legend.selectedMode == Legend.SelectedMode.Single) { for (int n = 0; n < datas.Count; n++) { OnLegendButtonClick(n, datas[n], n == 0 ? true : false); } } }
private void InitLegend() { m_Legend.OnChanged(); TextAnchor anchor = m_Legend.location.runtimeTextAnchor; Vector2 anchorMin = m_Legend.location.runtimeAnchorMin; Vector2 anchorMax = m_Legend.location.runtimeAnchorMax; Vector2 pivot = m_Legend.location.runtimePivot; var legendObject = ChartHelper.AddObject(s_LegendObjectName, transform, anchorMin, anchorMax, pivot, new Vector2(chartWidth, chartHeight)); legendObject.transform.localPosition = m_Legend.location.GetPosition(chartWidth, chartHeight); m_LegendRealShowName = m_Series.GetSerieNameList(); List <string> datas; if (m_Legend.show && m_Legend.data.Count > 0) { datas = new List <string>(); for (int i = 0; i < m_LegendRealShowName.Count; i++) { if (m_Legend.data.Contains(m_LegendRealShowName[i])) { datas.Add(m_LegendRealShowName[i]); } } } else { datas = m_LegendRealShowName; } int totalLegend = 0; for (int i = 0; i < datas.Count; i++) { if (!m_Series.IsLegalLegendName(datas[i])) { continue; } totalLegend++; } m_Legend.RemoveButton(); ChartHelper.DestroyAllChildren(legendObject.transform); if (!m_Legend.show) { return; } //天讯修改,保存当前Legend对象 List <Button> btns = new List <Button>(); for (int i = 0; i < datas.Count; i++) { if (!m_Series.IsLegalLegendName(datas[i])) { continue; } string legendName = m_Legend.GetFormatterContent(datas[i]); var readIndex = m_LegendRealShowName.IndexOf(datas[i]); var objName = s_LegendObjectName + "_" + i + "_" + datas[i]; Button btn = ChartHelper.AddButtonObject(objName, legendObject.transform, m_ThemeInfo.font, m_Legend.itemFontSize, m_ThemeInfo.legendTextColor, anchor, anchorMin, anchorMax, pivot, new Vector2(m_Legend.itemWidth, m_Legend.itemHeight)); var bgColor = IsActiveByLegend(datas[i]) ? m_ThemeInfo.GetColor(readIndex) : m_ThemeInfo.legendUnableColor; m_Legend.SetButton(legendName, btn, totalLegend); m_Legend.UpdateButtonColor(legendName, bgColor); btn.GetComponentInChildren <Text>().text = legendName; //天讯修改 btn.transform.localRotation = Quaternion.identity;//保证文字的角度为零 btns.Add(btn); ChartHelper.ClearEventListener(btn.gameObject); ChartHelper.AddEventListener(btn.gameObject, EventTriggerType.PointerDown, (data) => { if (data.selectedObject == null || m_Legend.selectedMode == Legend.SelectedMode.None) { return; } var temp = data.selectedObject.name.Split('_'); string selectedName = temp[2]; int clickedIndex = int.Parse(temp[1]); if (m_Legend.selectedMode == Legend.SelectedMode.Multiple) { OnLegendButtonClick(clickedIndex, selectedName, !IsActiveByLegend(selectedName)); } else { var btnList = m_Legend.buttonList.Values.ToArray(); if (btnList.Length == 1) { OnLegendButtonClick(0, selectedName, !IsActiveByLegend(selectedName)); } else { for (int n = 0; n < btnList.Length; n++) { temp = btnList[n].name.Split('_'); selectedName = temp[2]; var index = int.Parse(temp[1]); OnLegendButtonClick(n, selectedName, index == clickedIndex ? true : false); } } } }); ChartHelper.AddEventListener(btn.gameObject, EventTriggerType.PointerEnter, (data) => { if (btn == null) { return; } var temp = btn.name.Split('_'); string selectedName = temp[2]; int index = int.Parse(temp[1]); OnLegendButtonEnter(index, selectedName); }); ChartHelper.AddEventListener(btn.gameObject, EventTriggerType.PointerExit, (data) => { if (btn == null) { return; } var temp = btn.name.Split('_'); string selectedName = temp[2]; int index = int.Parse(temp[1]); OnLegendButtonExit(index, selectedName); }); } if (m_Legend.selectedMode == Legend.SelectedMode.Single) { for (int n = 0; n < m_LegendRealShowName.Count; n++) { OnLegendButtonClick(n, m_LegendRealShowName[n], n == 0 ? true : false); } } //天讯修改,当高度和者宽度设置-1时,设置控制自适应内容 if (m_Legend.itemWidth == -1 && m_Legend.itemHeight == -1) { btns.Sort((l, r) => { var lTextLength = l.GetComponentInChildren <Text>().text.Length; var rTextLength = r.GetComponentInChildren <Text>().text.Length; return(lTextLength == rTextLength ? 0 : lTextLength < rTextLength ? 1 : -1); }); m_Legend.itemWidth = btns[0].GetComponentInChildren <Text>().preferredWidth + 15;; m_Legend.itemHeight = btns[0].GetComponentInChildren <Text>().preferredHeight + 5;; foreach (Button button in btns) { var btnTransform = button.transform as RectTransform; btnTransform.sizeDelta = new Vector2(m_Legend.itemWidth, m_Legend.itemHeight); } } }
private void InitLegend() { m_Legend.OnChanged(); ChartHelper.HideAllObject(transform, s_LegendObjectName); TextAnchor anchor = m_Legend.location.textAnchor; Vector2 anchorMin = m_Legend.location.anchorMin; Vector2 anchorMax = m_Legend.location.anchorMax; Vector2 pivot = m_Legend.location.pivot; var legendObject = ChartHelper.AddObject(s_LegendObjectName, transform, anchorMin, anchorMax, pivot, new Vector2(chartWidth, chartHeight)); legendObject.transform.localPosition = m_Legend.location.GetPosition(chartWidth, chartHeight); ChartHelper.HideAllObject(legendObject, s_LegendObjectName); var serieNameList = m_Series.GetSerieNameList(); List <string> datas; if (m_Legend.data.Count > 0) { datas = new List <string>(); for (int i = 0; i < m_Legend.data.Count; i++) { var category = m_Legend.data[i]; if (serieNameList.Contains(category)) { datas.Add(category); } } } else { datas = serieNameList; } m_Legend.RemoveButton(); for (int i = 0; i < datas.Count; i++) { string legendName = datas[i]; Button btn = ChartHelper.AddButtonObject(s_LegendObjectName + "_" + i + "_" + legendName, legendObject.transform, m_ThemeInfo.font, m_Legend.itemFontSize, m_ThemeInfo.legendTextColor, anchor, anchorMin, anchorMax, pivot, new Vector2(m_Legend.itemWidth, m_Legend.itemHeight)); var bgColor = IsLegendActive(legendName) ? m_ThemeInfo.GetColor(i) : m_ThemeInfo.legendUnableColor; m_Legend.SetButton(legendName, btn, datas.Count); m_Legend.UpdateButtonColor(legendName, bgColor); btn.GetComponentInChildren <Text>().text = legendName; ChartHelper.ClearEventListener(btn.gameObject); ChartHelper.AddEventListener(btn.gameObject, EventTriggerType.PointerDown, (data) => { if (data.selectedObject == null) { return; } var temp = data.selectedObject.name.Split('_'); string selectedName = temp[2]; int index = int.Parse(temp[1]); OnLegendButtonClick(index, selectedName); }); ChartHelper.AddEventListener(btn.gameObject, EventTriggerType.PointerEnter, (data) => { if (btn == null) { return; } var temp = btn.name.Split('_'); string selectedName = temp[2]; int index = int.Parse(temp[1]); OnLegendButtonEnter(index, selectedName); }); ChartHelper.AddEventListener(btn.gameObject, EventTriggerType.PointerExit, (data) => { if (btn == null) { return; } var temp = btn.name.Split('_'); string selectedName = temp[2]; int index = int.Parse(temp[1]); OnLegendButtonExit(index, selectedName); }); } }