1a3e0fd82Sopenharmony_ci/* 2a3e0fd82Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. 3a3e0fd82Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4a3e0fd82Sopenharmony_ci * you may not use this file except in compliance with the License. 5a3e0fd82Sopenharmony_ci * You may obtain a copy of the License at 6a3e0fd82Sopenharmony_ci * 7a3e0fd82Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8a3e0fd82Sopenharmony_ci * 9a3e0fd82Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10a3e0fd82Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11a3e0fd82Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12a3e0fd82Sopenharmony_ci * See the License for the specific language governing permissions and 13a3e0fd82Sopenharmony_ci * limitations under the License. 14a3e0fd82Sopenharmony_ci */ 15a3e0fd82Sopenharmony_ci 16a3e0fd82Sopenharmony_ci#include "components/ui_axis.h" 17a3e0fd82Sopenharmony_ci#include "common/screen.h" 18a3e0fd82Sopenharmony_ci#include "engines/gfx/gfx_engine_manager.h" 19a3e0fd82Sopenharmony_ci 20a3e0fd82Sopenharmony_cinamespace OHOS { 21a3e0fd82Sopenharmony_ciUIAxis::UIAxis() 22a3e0fd82Sopenharmony_ci : maxRange_(0), 23a3e0fd82Sopenharmony_ci minRange_(0), 24a3e0fd82Sopenharmony_ci start_({0, 0}), 25a3e0fd82Sopenharmony_ci end_({0, 0}), 26a3e0fd82Sopenharmony_ci markInterval_(0), 27a3e0fd82Sopenharmony_ci dataPerMark_(0), 28a3e0fd82Sopenharmony_ci dataInterval_(0), 29a3e0fd82Sopenharmony_ci markDataCount_(AXIS_DEFAULT_MARK_INTERVAL), 30a3e0fd82Sopenharmony_ci enableReverse_(false) 31a3e0fd82Sopenharmony_ci{ 32a3e0fd82Sopenharmony_ci SetStyle(STYLE_LINE_WIDTH, 1); 33a3e0fd82Sopenharmony_ci SetStyle(STYLE_LINE_COLOR, Color::White().full); 34a3e0fd82Sopenharmony_ci} 35a3e0fd82Sopenharmony_ci 36a3e0fd82Sopenharmony_civoid UIAxis::SetLineColor(const ColorType& color) 37a3e0fd82Sopenharmony_ci{ 38a3e0fd82Sopenharmony_ci SetStyle(STYLE_LINE_COLOR, color.full); 39a3e0fd82Sopenharmony_ci} 40a3e0fd82Sopenharmony_ci 41a3e0fd82Sopenharmony_civoid UIXAxis::SetMarkNum(uint16_t count) 42a3e0fd82Sopenharmony_ci{ 43a3e0fd82Sopenharmony_ci if ((count == 0) || (count > Screen::GetInstance().GetWidth())) { 44a3e0fd82Sopenharmony_ci return; 45a3e0fd82Sopenharmony_ci } 46a3e0fd82Sopenharmony_ci markDataCount_ = count; 47a3e0fd82Sopenharmony_ci UpdateAxis(); 48a3e0fd82Sopenharmony_ci} 49a3e0fd82Sopenharmony_ci 50a3e0fd82Sopenharmony_cibool UIXAxis::SetDataRange(uint16_t min, uint16_t max) 51a3e0fd82Sopenharmony_ci{ 52a3e0fd82Sopenharmony_ci if (max <= min) { 53a3e0fd82Sopenharmony_ci return false; 54a3e0fd82Sopenharmony_ci } 55a3e0fd82Sopenharmony_ci maxRange_ = max; 56a3e0fd82Sopenharmony_ci minRange_ = min; 57a3e0fd82Sopenharmony_ci return UpdateAxis(); 58a3e0fd82Sopenharmony_ci} 59a3e0fd82Sopenharmony_ci 60a3e0fd82Sopenharmony_civoid UIXAxis::UpdateAxisPoints() 61a3e0fd82Sopenharmony_ci{ 62a3e0fd82Sopenharmony_ci Rect current = GetContentRect(); 63a3e0fd82Sopenharmony_ci start_.x = current.GetLeft(); 64a3e0fd82Sopenharmony_ci end_.x = current.GetRight(); 65a3e0fd82Sopenharmony_ci start_.y = enableReverse_ ? current.GetTop() : current.GetBottom(); 66a3e0fd82Sopenharmony_ci end_.y = start_.y; 67a3e0fd82Sopenharmony_ci} 68a3e0fd82Sopenharmony_ci 69a3e0fd82Sopenharmony_cibool UIXAxis::UpdateAxis() 70a3e0fd82Sopenharmony_ci{ 71a3e0fd82Sopenharmony_ci UpdateAxisPoints(); 72a3e0fd82Sopenharmony_ci int16_t xAxisLength = end_.x - start_.x + 1; 73a3e0fd82Sopenharmony_ci if (xAxisLength <= 0) { 74a3e0fd82Sopenharmony_ci return false; 75a3e0fd82Sopenharmony_ci } 76a3e0fd82Sopenharmony_ci 77a3e0fd82Sopenharmony_ci if (markDataCount_ != 0) { 78a3e0fd82Sopenharmony_ci dataInterval_ = static_cast<float>((maxRange_ - minRange_) / markDataCount_); 79a3e0fd82Sopenharmony_ci markInterval_ = static_cast<float>(xAxisLength) / markDataCount_; 80a3e0fd82Sopenharmony_ci if (maxRange_ > minRange_) { 81a3e0fd82Sopenharmony_ci dataPerMark_ = markInterval_ / dataInterval_; 82a3e0fd82Sopenharmony_ci } 83a3e0fd82Sopenharmony_ci } 84a3e0fd82Sopenharmony_ci 85a3e0fd82Sopenharmony_ci return true; 86a3e0fd82Sopenharmony_ci} 87a3e0fd82Sopenharmony_ci 88a3e0fd82Sopenharmony_civoid UIXAxis::TranslateToPixel(int16_t& value) 89a3e0fd82Sopenharmony_ci{ 90a3e0fd82Sopenharmony_ci float minXStep = dataPerMark_ ? dataPerMark_ : markInterval_; 91a3e0fd82Sopenharmony_ci value = start_.x + static_cast<int16_t>((value - minRange_) * minXStep); 92a3e0fd82Sopenharmony_ci} 93a3e0fd82Sopenharmony_ci 94a3e0fd82Sopenharmony_civoid UIAxis::OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) 95a3e0fd82Sopenharmony_ci{ 96a3e0fd82Sopenharmony_ci BaseGfxEngine::GetInstance()->DrawLine(gfxDstBuffer, start_, end_, invalidatedArea, style_->lineWidth_, 97a3e0fd82Sopenharmony_ci style_->lineColor_, style_->lineOpa_); 98a3e0fd82Sopenharmony_ci DrawAxisMark(gfxDstBuffer, invalidatedArea); 99a3e0fd82Sopenharmony_ci} 100a3e0fd82Sopenharmony_ci 101a3e0fd82Sopenharmony_civoid UIXAxis::DrawAxisMark(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) 102a3e0fd82Sopenharmony_ci{ 103a3e0fd82Sopenharmony_ci Point start; 104a3e0fd82Sopenharmony_ci Point end; 105a3e0fd82Sopenharmony_ci uint16_t index = 1; 106a3e0fd82Sopenharmony_ci while (index <= markDataCount_) { 107a3e0fd82Sopenharmony_ci start.y = start_.y; 108a3e0fd82Sopenharmony_ci start.x = start_.x + static_cast<int16_t>(index * markInterval_); 109a3e0fd82Sopenharmony_ci end.y = enableReverse_ ? (start.y + AXIS_DEFAULT_MARK_LENGTH) : (start.y - AXIS_DEFAULT_MARK_LENGTH); 110a3e0fd82Sopenharmony_ci end.x = start.x; 111a3e0fd82Sopenharmony_ci 112a3e0fd82Sopenharmony_ci BaseGfxEngine::GetInstance()->DrawLine(gfxDstBuffer, start, end, invalidatedArea, 113a3e0fd82Sopenharmony_ci style_->lineWidth_, style_->lineColor_, style_->lineOpa_); 114a3e0fd82Sopenharmony_ci index++; 115a3e0fd82Sopenharmony_ci } 116a3e0fd82Sopenharmony_ci} 117a3e0fd82Sopenharmony_ci 118a3e0fd82Sopenharmony_civoid UIYAxis::SetMarkNum(uint16_t count) 119a3e0fd82Sopenharmony_ci{ 120a3e0fd82Sopenharmony_ci if ((count == 0) || (count > Screen::GetInstance().GetHeight())) { 121a3e0fd82Sopenharmony_ci return; 122a3e0fd82Sopenharmony_ci } 123a3e0fd82Sopenharmony_ci markDataCount_ = count; 124a3e0fd82Sopenharmony_ci dataInterval_ = static_cast<float>((maxRange_ - minRange_) / markDataCount_); 125a3e0fd82Sopenharmony_ci} 126a3e0fd82Sopenharmony_ci 127a3e0fd82Sopenharmony_cibool UIYAxis::SetDataRange(uint16_t min, uint16_t max) 128a3e0fd82Sopenharmony_ci{ 129a3e0fd82Sopenharmony_ci if (max <= min) { 130a3e0fd82Sopenharmony_ci return false; 131a3e0fd82Sopenharmony_ci } 132a3e0fd82Sopenharmony_ci 133a3e0fd82Sopenharmony_ci maxRange_ = max; 134a3e0fd82Sopenharmony_ci minRange_ = min; 135a3e0fd82Sopenharmony_ci return UpdateAxis(); 136a3e0fd82Sopenharmony_ci} 137a3e0fd82Sopenharmony_ci 138a3e0fd82Sopenharmony_civoid UIYAxis::UpdateAxisPoints() 139a3e0fd82Sopenharmony_ci{ 140a3e0fd82Sopenharmony_ci Rect current = GetContentRect(); 141a3e0fd82Sopenharmony_ci int16_t top = current.GetTop(); 142a3e0fd82Sopenharmony_ci int16_t bottom = current.GetBottom(); 143a3e0fd82Sopenharmony_ci 144a3e0fd82Sopenharmony_ci start_.x = current.GetLeft(); 145a3e0fd82Sopenharmony_ci end_.x = start_.x; 146a3e0fd82Sopenharmony_ci if (enableReverse_) { 147a3e0fd82Sopenharmony_ci start_.y = top; 148a3e0fd82Sopenharmony_ci end_.y = bottom; 149a3e0fd82Sopenharmony_ci } else { 150a3e0fd82Sopenharmony_ci start_.y = bottom; 151a3e0fd82Sopenharmony_ci end_.y = top; 152a3e0fd82Sopenharmony_ci } 153a3e0fd82Sopenharmony_ci} 154a3e0fd82Sopenharmony_ci 155a3e0fd82Sopenharmony_civoid UIYAxis::TranslateToPixel(int16_t& value) 156a3e0fd82Sopenharmony_ci{ 157a3e0fd82Sopenharmony_ci float minYStep = dataPerMark_ ? dataPerMark_ : markInterval_; 158a3e0fd82Sopenharmony_ci if (enableReverse_) { 159a3e0fd82Sopenharmony_ci value = start_.y + static_cast<int16_t>((maxRange_ - value + minRange_) * minYStep); 160a3e0fd82Sopenharmony_ci } else { 161a3e0fd82Sopenharmony_ci value = start_.y - static_cast<int16_t>((value - minRange_) * minYStep); 162a3e0fd82Sopenharmony_ci } 163a3e0fd82Sopenharmony_ci} 164a3e0fd82Sopenharmony_ci 165a3e0fd82Sopenharmony_cibool UIYAxis::UpdateAxis() 166a3e0fd82Sopenharmony_ci{ 167a3e0fd82Sopenharmony_ci UpdateAxisPoints(); 168a3e0fd82Sopenharmony_ci int16_t yAxisLength = enableReverse_ ? (end_.y - start_.y + 1) : (start_.y - end_.y + 1); 169a3e0fd82Sopenharmony_ci if (yAxisLength <= 0) { 170a3e0fd82Sopenharmony_ci return false; 171a3e0fd82Sopenharmony_ci } 172a3e0fd82Sopenharmony_ci 173a3e0fd82Sopenharmony_ci if (markDataCount_ != 0) { 174a3e0fd82Sopenharmony_ci dataInterval_ = static_cast<float>((maxRange_ - minRange_) / markDataCount_); 175a3e0fd82Sopenharmony_ci markInterval_ = static_cast<float>(yAxisLength) / markDataCount_; 176a3e0fd82Sopenharmony_ci if (dataInterval_ != 0) { 177a3e0fd82Sopenharmony_ci dataPerMark_ = markInterval_ / dataInterval_; 178a3e0fd82Sopenharmony_ci } 179a3e0fd82Sopenharmony_ci } 180a3e0fd82Sopenharmony_ci return true; 181a3e0fd82Sopenharmony_ci} 182a3e0fd82Sopenharmony_ci 183a3e0fd82Sopenharmony_civoid UIYAxis::DrawAxisMark(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) 184a3e0fd82Sopenharmony_ci{ 185a3e0fd82Sopenharmony_ci uint16_t index = 1; 186a3e0fd82Sopenharmony_ci while (index <= markDataCount_) { 187a3e0fd82Sopenharmony_ci Point start; 188a3e0fd82Sopenharmony_ci Point end; 189a3e0fd82Sopenharmony_ci start.x = start_.x; 190a3e0fd82Sopenharmony_ci start.y = enableReverse_ ? (start_.y + static_cast<int16_t>(index * markInterval_)) 191a3e0fd82Sopenharmony_ci : (start_.y - static_cast<int16_t>(index * markInterval_)); 192a3e0fd82Sopenharmony_ci end.x = start.x + AXIS_DEFAULT_MARK_LENGTH; 193a3e0fd82Sopenharmony_ci end.y = start.y; 194a3e0fd82Sopenharmony_ci 195a3e0fd82Sopenharmony_ci BaseGfxEngine::GetInstance()->DrawLine(gfxDstBuffer, start, end, invalidatedArea, 196a3e0fd82Sopenharmony_ci style_->lineWidth_, style_->lineColor_, style_->lineOpa_); 197a3e0fd82Sopenharmony_ci index++; 198a3e0fd82Sopenharmony_ci } 199a3e0fd82Sopenharmony_ci} 200a3e0fd82Sopenharmony_ci} // namespace OHOS 201